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;
205 sc->command_mask = 0;
207 sc->kbd.head = sc->kbd.tail = 0;
208 sc->aux.head = sc->aux.tail = 0;
210 sc->kbd.call_count = 0;
211 sc->kbd.qcount = sc->kbd.max_qcount = 0;
212 sc->aux.call_count = 0;
213 sc->aux.qcount = sc->aux.max_qcount = 0;
222 /* open a keyboard controller */
224 atkbdc_open(int unit)
230 if ((atkbdc_softc[unit]->port0 != NULL)
231 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
232 return (KBDC)atkbdc_softc[unit];
237 * I/O access arbitration in `kbdio'
239 * The `kbdio' module uses a simplistic convention to arbitrate
240 * I/O access to the controller/keyboard/mouse. The convention requires
241 * close cooperation of the calling device driver.
243 * The device drivers which utilize the `kbdio' module are assumed to
244 * have the following set of routines.
245 * a. An interrupt handler (the bottom half of the driver).
246 * b. Timeout routines which may briefly poll the keyboard controller.
247 * c. Routines outside interrupt context (the top half of the driver).
248 * They should follow the rules below:
249 * 1. The interrupt handler may assume that it always has full access
250 * to the controller/keyboard/mouse.
251 * 2. The other routines must issue `spltty()' if they wish to
252 * prevent the interrupt handler from accessing
253 * the controller/keyboard/mouse.
254 * 3. The timeout routines and the top half routines of the device driver
255 * arbitrate I/O access by observing the lock flag in `kbdio'.
256 * The flag is manipulated via `kbdc_lock()'; when one wants to
257 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
258 * the call returns with TRUE. Otherwise the caller must back off.
259 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
260 * is finished. This mechanism does not prevent the interrupt
261 * handler from being invoked at any time and carrying out I/O.
262 * Therefore, `spltty()' must be strategically placed in the device
263 * driver code. Also note that the timeout routine may interrupt
264 * `kbdc_lock()' called by the top half of the driver, but this
265 * interruption is OK so long as the timeout routine observes
267 * 4. The interrupt and timeout routines should not extend I/O operation
268 * across more than one interrupt or timeout; they must complete any
269 * necessary I/O operation within one invocation of the routine.
270 * This means that if the timeout routine acquires the lock flag,
271 * it must reset the flag to FALSE before it returns.
274 /* set/reset polling lock */
276 kbdc_lock(KBDC p, int lock)
280 prevlock = kbdcp(p)->lock;
281 kbdcp(p)->lock = lock;
283 return (prevlock != lock);
286 /* check if any data is waiting to be processed */
288 kbdc_data_ready(KBDC p)
290 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
291 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
294 /* queuing functions */
297 addq(kbdkqueue *q, int c)
299 if (nextq(q->tail) != q->head) {
301 q->tail = nextq(q->tail);
305 if (q->qcount > q->max_qcount)
306 q->max_qcount = q->qcount;
314 removeq(kbdkqueue *q)
318 if (q->tail != q->head) {
320 q->head = nextq(q->head);
330 * device I/O routines
333 wait_while_controller_busy(struct atkbdc_softc *kbdc)
335 /* CPU will stay inside the loop for 100msec at most */
336 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);
342 addq(&kbdc->kbd, read_data(kbdc));
343 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
344 DELAY(KBDD_DELAYTIME);
345 addq(&kbdc->aux, read_data(kbdc));
347 DELAY(KBDC_DELAYTIME);
348 if (CHECKTIMEOUT(&retry))
355 * wait for any data; whether it's from the controller,
356 * the keyboard, or the aux device.
359 wait_for_data(struct atkbdc_softc *kbdc)
361 /* CPU will stay inside the loop for 200msec at most */
362 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
365 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
366 DELAY(KBDC_DELAYTIME);
367 if (CHECKTIMEOUT(&retry))
370 DELAY(KBDD_DELAYTIME);
374 /* wait for data from the keyboard */
376 wait_for_kbd_data(struct atkbdc_softc *kbdc)
378 /* CPU will stay inside the loop for 200msec at most */
379 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
382 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
383 != KBDS_KBD_BUFFER_FULL) {
384 if (f == KBDS_AUX_BUFFER_FULL) {
385 DELAY(KBDD_DELAYTIME);
386 addq(&kbdc->aux, read_data(kbdc));
388 DELAY(KBDC_DELAYTIME);
389 if (CHECKTIMEOUT(&retry))
392 DELAY(KBDD_DELAYTIME);
397 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
398 * queue anything else.
401 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
403 /* CPU will stay inside the loop for 200msec at most */
404 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
408 while (CHECKTIMEOUT(&retry) == 0) {
409 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
410 DELAY(KBDD_DELAYTIME);
412 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
413 if ((b == KBD_ACK) || (b == KBD_RESEND)
414 || (b == KBD_RESET_FAIL))
417 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
421 DELAY(KBDC_DELAYTIME);
426 /* wait for data from the aux device */
428 wait_for_aux_data(struct atkbdc_softc *kbdc)
430 /* CPU will stay inside the loop for 200msec at most */
431 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
434 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
435 != KBDS_AUX_BUFFER_FULL) {
436 if (f == KBDS_KBD_BUFFER_FULL) {
437 DELAY(KBDD_DELAYTIME);
438 addq(&kbdc->kbd, read_data(kbdc));
440 DELAY(KBDC_DELAYTIME);
441 if (CHECKTIMEOUT(&retry))
444 DELAY(KBDD_DELAYTIME);
449 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
450 * queue anything else.
453 wait_for_aux_ack(struct atkbdc_softc *kbdc)
455 /* CPU will stay inside the loop for 200msec at most */
456 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
460 while (CHECKTIMEOUT(&retry) == 0) {
461 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
462 DELAY(KBDD_DELAYTIME);
464 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
465 if ((b == PSM_ACK) || (b == PSM_RESEND)
466 || (b == PSM_RESET_FAIL))
469 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
473 DELAY(KBDC_DELAYTIME);
478 /* write a one byte command to the controller */
480 write_controller_command(KBDC p, int c)
482 if (!wait_while_controller_busy(kbdcp(p)))
484 write_command(kbdcp(p), c);
488 /* write a one byte data to the controller */
490 write_controller_data(KBDC p, int c)
492 if (!wait_while_controller_busy(kbdcp(p)))
494 write_data(kbdcp(p), c);
498 /* write a one byte keyboard command */
500 write_kbd_command(KBDC p, int c)
502 if (!wait_while_controller_busy(kbdcp(p)))
504 write_data(kbdcp(p), c);
508 /* write a one byte auxiliary device command */
510 write_aux_command(KBDC p, int c)
512 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
514 return write_controller_data(p, c);
517 /* send a command to the keyboard and wait for ACK */
519 send_kbd_command(KBDC p, int c)
521 int retry = KBD_MAXRETRY;
524 while (retry-- > 0) {
525 if (!write_kbd_command(p, c))
527 res = wait_for_kbd_ack(kbdcp(p));
534 /* send a command to the auxiliary device and wait for ACK */
536 send_aux_command(KBDC p, int c)
538 int retry = KBD_MAXRETRY;
541 while (retry-- > 0) {
542 if (!write_aux_command(p, c))
546 * The aux device may have already sent one or two bytes of
547 * status data, when a command is received. It will immediately
548 * stop data transmission, thus, leaving an incomplete data
549 * packet in our buffer. We have to discard any unprocessed
550 * data in order to remove such packets. Well, we may remove
551 * unprocessed, but necessary data byte as well...
553 emptyq(&kbdcp(p)->aux);
554 res = wait_for_aux_ack(kbdcp(p));
561 /* send a command and a data to the keyboard, wait for ACKs */
563 send_kbd_command_and_data(KBDC p, int c, int d)
568 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
569 if (!write_kbd_command(p, c))
571 res = wait_for_kbd_ack(kbdcp(p));
574 else if (res != KBD_RESEND)
580 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
581 if (!write_kbd_command(p, d))
583 res = wait_for_kbd_ack(kbdcp(p));
584 if (res != KBD_RESEND)
590 /* send a command and a data to the auxiliary device, wait for ACKs */
592 send_aux_command_and_data(KBDC p, int c, int d)
597 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
598 if (!write_aux_command(p, c))
600 emptyq(&kbdcp(p)->aux);
601 res = wait_for_aux_ack(kbdcp(p));
604 else if (res != PSM_RESEND)
610 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
611 if (!write_aux_command(p, d))
613 res = wait_for_aux_ack(kbdcp(p));
614 if (res != PSM_RESEND)
621 * read one byte from any source; whether from the controller,
622 * the keyboard, or the aux device
625 read_controller_data(KBDC p)
627 if (availq(&kbdcp(p)->kbd))
628 return removeq(&kbdcp(p)->kbd);
629 if (availq(&kbdcp(p)->aux))
630 return removeq(&kbdcp(p)->aux);
631 if (!wait_for_data(kbdcp(p)))
632 return -1; /* timeout */
633 return read_data(kbdcp(p));
640 /* read one byte from the keyboard */
642 read_kbd_data(KBDC p)
647 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
648 "aux q: %d calls, max %d chars\n",
649 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
650 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
654 if (availq(&kbdcp(p)->kbd))
655 return removeq(&kbdcp(p)->kbd);
656 if (!wait_for_kbd_data(kbdcp(p)))
657 return -1; /* timeout */
658 return read_data(kbdcp(p));
661 /* read one byte from the keyboard, but return immediately if
665 read_kbd_data_no_wait(KBDC p)
672 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
673 "aux q: %d calls, max %d chars\n",
674 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
675 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
679 if (availq(&kbdcp(p)->kbd))
680 return removeq(&kbdcp(p)->kbd);
681 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
682 if (f == KBDS_AUX_BUFFER_FULL) {
683 DELAY(KBDD_DELAYTIME);
684 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
685 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
687 if (f == KBDS_KBD_BUFFER_FULL) {
688 DELAY(KBDD_DELAYTIME);
689 return read_data(kbdcp(p));
691 return -1; /* no data */
694 /* read one byte from the aux device */
696 read_aux_data(KBDC p)
698 if (availq(&kbdcp(p)->aux))
699 return removeq(&kbdcp(p)->aux);
700 if (!wait_for_aux_data(kbdcp(p)))
701 return -1; /* timeout */
702 return read_data(kbdcp(p));
705 /* read one byte from the aux device, but return immediately if
709 read_aux_data_no_wait(KBDC p)
713 if (availq(&kbdcp(p)->aux))
714 return removeq(&kbdcp(p)->aux);
715 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
716 if (f == KBDS_KBD_BUFFER_FULL) {
717 DELAY(KBDD_DELAYTIME);
718 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
719 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
721 if (f == KBDS_AUX_BUFFER_FULL) {
722 DELAY(KBDD_DELAYTIME);
723 return read_data(kbdcp(p));
725 return -1; /* no data */
728 /* discard data from the keyboard */
730 empty_kbd_buffer(KBDC p, int wait)
741 for (t = wait; t > 0; ) {
742 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
743 DELAY(KBDD_DELAYTIME);
744 b = read_data(kbdcp(p));
745 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
746 addq(&kbdcp(p)->aux, b);
760 if ((c1 > 0) || (c2 > 0))
761 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
764 emptyq(&kbdcp(p)->kbd);
767 /* discard data from the aux device */
769 empty_aux_buffer(KBDC p, int wait)
780 for (t = wait; t > 0; ) {
781 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
782 DELAY(KBDD_DELAYTIME);
783 b = read_data(kbdcp(p));
784 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
785 addq(&kbdcp(p)->kbd, b);
799 if ((c1 > 0) || (c2 > 0))
800 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
803 emptyq(&kbdcp(p)->aux);
806 /* discard any data from the keyboard or the aux device */
808 empty_both_buffers(KBDC p, int wait)
818 for (t = wait; t > 0; ) {
819 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
820 DELAY(KBDD_DELAYTIME);
821 (void)read_data(kbdcp(p));
823 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
835 if ((c1 > 0) || (c2 > 0))
836 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
839 emptyq(&kbdcp(p)->kbd);
840 emptyq(&kbdcp(p)->aux);
843 /* keyboard and mouse device control */
845 /* NOTE: enable the keyboard port but disable the keyboard
846 * interrupt before calling "reset_kbd()".
851 int retry = KBD_MAXRETRY;
852 int again = KBD_MAXWAIT;
853 int c = KBD_RESEND; /* keep the compiler happy */
855 while (retry-- > 0) {
856 empty_both_buffers(p, 10);
857 if (!write_kbd_command(p, KBDC_RESET_KBD))
859 emptyq(&kbdcp(p)->kbd);
860 c = read_controller_data(p);
861 if (verbose || bootverbose)
862 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
863 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
869 while (again-- > 0) {
870 /* wait awhile, well, in fact we must wait quite loooooooooooong */
871 DELAY(KBD_RESETDELAY*1000);
872 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
873 if (c != -1) /* wait again if the controller is not ready */
876 if (verbose || bootverbose)
877 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
878 if (c != KBD_RESET_DONE)
883 /* NOTE: enable the aux port but disable the aux interrupt
884 * before calling `reset_aux_dev()'.
887 reset_aux_dev(KBDC p)
889 int retry = KBD_MAXRETRY;
890 int again = KBD_MAXWAIT;
891 int c = PSM_RESEND; /* keep the compiler happy */
893 while (retry-- > 0) {
894 empty_both_buffers(p, 10);
895 if (!write_aux_command(p, PSMC_RESET_DEV))
897 emptyq(&kbdcp(p)->aux);
898 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
899 for (again = KBD_MAXWAIT; again > 0; --again) {
900 DELAY(KBD_RESETDELAY*1000);
901 c = read_aux_data_no_wait(p);
905 if (verbose || bootverbose)
906 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
907 if (c == PSM_ACK) /* aux dev is about to reset... */
913 for (again = KBD_MAXWAIT; again > 0; --again) {
914 /* wait awhile, well, quite looooooooooooong */
915 DELAY(KBD_RESETDELAY*1000);
916 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
917 if (c != -1) /* wait again if the controller is not ready */
920 if (verbose || bootverbose)
921 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
922 if (c != PSM_RESET_DONE) /* reset status */
925 c = read_aux_data(p); /* device ID */
926 if (verbose || bootverbose)
927 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
928 /* NOTE: we could check the device ID now, but leave it later... */
932 /* controller diagnostics and setup */
935 test_controller(KBDC p)
937 int retry = KBD_MAXRETRY;
938 int again = KBD_MAXWAIT;
939 int c = KBD_DIAG_FAIL;
941 while (retry-- > 0) {
942 empty_both_buffers(p, 10);
943 if (write_controller_command(p, KBDC_DIAGNOSE))
949 emptyq(&kbdcp(p)->kbd);
950 while (again-- > 0) {
952 DELAY(KBD_RESETDELAY*1000);
953 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
954 if (c != -1) /* wait again if the controller is not ready */
957 if (verbose || bootverbose)
958 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
959 return (c == KBD_DIAG_DONE);
963 test_kbd_port(KBDC p)
965 int retry = KBD_MAXRETRY;
966 int again = KBD_MAXWAIT;
969 while (retry-- > 0) {
970 empty_both_buffers(p, 10);
971 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
977 emptyq(&kbdcp(p)->kbd);
978 while (again-- > 0) {
979 c = read_controller_data(p);
980 if (c != -1) /* try again if the controller is not ready */
983 if (verbose || bootverbose)
984 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
989 test_aux_port(KBDC p)
991 int retry = KBD_MAXRETRY;
992 int again = KBD_MAXWAIT;
995 while (retry-- > 0) {
996 empty_both_buffers(p, 10);
997 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1003 emptyq(&kbdcp(p)->kbd);
1004 while (again-- > 0) {
1005 c = read_controller_data(p);
1006 if (c != -1) /* try again if the controller is not ready */
1009 if (verbose || bootverbose)
1010 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1015 kbdc_get_device_mask(KBDC p)
1017 return kbdcp(p)->command_mask;
1021 kbdc_set_device_mask(KBDC p, int mask)
1023 kbdcp(p)->command_mask =
1024 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1028 get_controller_command_byte(KBDC p)
1030 if (kbdcp(p)->command_byte != -1)
1031 return kbdcp(p)->command_byte;
1032 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1034 emptyq(&kbdcp(p)->kbd);
1035 kbdcp(p)->command_byte = read_controller_data(p);
1036 return kbdcp(p)->command_byte;
1040 set_controller_command_byte(KBDC p, int mask, int command)
1042 if (get_controller_command_byte(p) == -1)
1045 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1046 if (command & KBD_DISABLE_KBD_PORT) {
1047 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1050 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1052 if (!write_controller_data(p, command))
1054 kbdcp(p)->command_byte = command;
1057 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",