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>
47 #include <dev/atkbdc/atkbdcreg.h>
50 #include <dev/ofw/openfirm.h>
51 #include <machine/bus_private.h>
52 #include <machine/ofw_machdep.h>
54 #include <isa/isareg.h>
59 #define MAXKBDC 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;
97 static struct bus_space_tag atkbdc_bst_store[MAXKBDC];
100 /* function prototypes */
102 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
103 bus_space_handle_t h0, bus_space_handle_t h1);
104 static int addq(kqueue *q, int c);
105 static int removeq(kqueue *q);
106 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
107 static int wait_for_data(atkbdc_softc_t *kbdc);
108 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
109 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
110 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
111 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
114 *atkbdc_get_softc(int unit)
118 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
120 sc = atkbdc_softc[unit];
122 sc = atkbdc_softc[unit]
123 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO);
131 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
133 if (rman_get_start(port0) <= 0)
135 if (rman_get_start(port1) <= 0)
141 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
142 struct resource *port1)
144 return atkbdc_setup(sc, rman_get_bustag(port0),
145 rman_get_bushandle(port0),
146 rman_get_bushandle(port1));
149 /* the backdoor to the keyboard controller! XXX */
151 atkbdc_configure(void)
154 bus_space_handle_t h0;
155 bus_space_handle_t h1;
156 #if defined(__i386__)
162 phandle_t chosen, node;
172 /* XXX: tag should be passed from the caller */
173 #if defined(__i386__)
174 tag = I386_BUS_SPACE_IO;
175 #elif defined(__amd64__)
176 tag = AMD64_BUS_SPACE_IO;
177 #elif defined(__ia64__)
178 tag = IA64_BUS_SPACE_IO;
179 #elif defined(__sparc64__)
180 tag = &atkbdc_bst_store[0];
186 if ((chosen = OF_finddevice("/chosen")) == -1)
188 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
190 if ((node = OF_instance_to_package(stdin)) == -1)
192 if (OF_getprop(node, "name", name, sizeof(name)) == -1)
194 name[sizeof(name) - 1] = '\0';
195 if (strcmp(name, "kb_ps2") != 0)
198 * The stdin handle points to an instance of a PS/2 keyboard
199 * package but we want the 8042 controller, which is the parent
200 * of that keyboard node.
202 if ((node = OF_parent(node)) == 0)
204 if (OF_decode_addr(node, 0, &space, &port0) != 0)
206 h0 = sparc64_fake_bustag(space, port0, tag);
207 bus_space_subregion(tag, h0, KBD_DATA_PORT, 1, &h0);
208 if (OF_decode_addr(node, 1, &space, &port1) != 0)
210 h1 = sparc64_fake_bustag(space, port1, tag);
211 bus_space_subregion(tag, h1, KBD_STATUS_PORT, 1, &h1);
214 resource_int_value("atkbdc", 0, "port", &port0);
215 port1 = IO_KBD + KBD_STATUS_PORT;
217 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
218 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
220 h0 = (bus_space_handle_t)port0;
221 h1 = (bus_space_handle_t)port1;
225 #if defined(__i386__)
227 * Check if we really have AT keyboard controller. Poll status
228 * register until we get "all clear" indication. If no such
229 * indication comes, it probably means that there is no AT
230 * keyboard controller present. Give up in such case. Check relies
231 * on the fact that reading from non-existing in/out port returns
232 * 0xff on i386. May or may not be true on other platforms.
234 flags = intr_disable();
235 for (i = 0; i != 65535; i++) {
236 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
244 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
248 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
249 bus_space_handle_t h1)
251 if (sc->ioh0 == 0) { /* XXX */
252 sc->command_byte = -1;
253 sc->command_mask = 0;
255 sc->kbd.head = sc->kbd.tail = 0;
256 sc->aux.head = sc->aux.tail = 0;
258 sc->kbd.call_count = 0;
259 sc->kbd.qcount = sc->kbd.max_qcount = 0;
260 sc->aux.call_count = 0;
261 sc->aux.qcount = sc->aux.max_qcount = 0;
270 /* open a keyboard controller */
272 atkbdc_open(int unit)
278 if ((atkbdc_softc[unit]->port0 != NULL)
279 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
280 return (KBDC)atkbdc_softc[unit];
285 * I/O access arbitration in `kbdio'
287 * The `kbdio' module uses a simplistic convention to arbitrate
288 * I/O access to the controller/keyboard/mouse. The convention requires
289 * close cooperation of the calling device driver.
291 * The device drivers which utilize the `kbdio' module are assumed to
292 * have the following set of routines.
293 * a. An interrupt handler (the bottom half of the driver).
294 * b. Timeout routines which may briefly poll the keyboard controller.
295 * c. Routines outside interrupt context (the top half of the driver).
296 * They should follow the rules below:
297 * 1. The interrupt handler may assume that it always has full access
298 * to the controller/keyboard/mouse.
299 * 2. The other routines must issue `spltty()' if they wish to
300 * prevent the interrupt handler from accessing
301 * the controller/keyboard/mouse.
302 * 3. The timeout routines and the top half routines of the device driver
303 * arbitrate I/O access by observing the lock flag in `kbdio'.
304 * The flag is manipulated via `kbdc_lock()'; when one wants to
305 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
306 * the call returns with TRUE. Otherwise the caller must back off.
307 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
308 * is finished. This mechanism does not prevent the interrupt
309 * handler from being invoked at any time and carrying out I/O.
310 * Therefore, `spltty()' must be strategically placed in the device
311 * driver code. Also note that the timeout routine may interrupt
312 * `kbdc_lock()' called by the top half of the driver, but this
313 * interruption is OK so long as the timeout routine observes
315 * 4. The interrupt and timeout routines should not extend I/O operation
316 * across more than one interrupt or timeout; they must complete any
317 * necessary I/O operation within one invocation of the routine.
318 * This means that if the timeout routine acquires the lock flag,
319 * it must reset the flag to FALSE before it returns.
322 /* set/reset polling lock */
324 kbdc_lock(KBDC p, int lock)
328 prevlock = kbdcp(p)->lock;
329 kbdcp(p)->lock = lock;
331 return (prevlock != lock);
334 /* check if any data is waiting to be processed */
336 kbdc_data_ready(KBDC p)
338 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
339 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
342 /* queuing functions */
345 addq(kqueue *q, int c)
347 if (nextq(q->tail) != q->head) {
349 q->tail = nextq(q->tail);
353 if (q->qcount > q->max_qcount)
354 q->max_qcount = q->qcount;
366 if (q->tail != q->head) {
368 q->head = nextq(q->head);
378 * device I/O routines
381 wait_while_controller_busy(struct atkbdc_softc *kbdc)
383 /* CPU will stay inside the loop for 100msec at most */
387 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
388 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
389 DELAY(KBDD_DELAYTIME);
390 addq(&kbdc->kbd, read_data(kbdc));
391 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
392 DELAY(KBDD_DELAYTIME);
393 addq(&kbdc->aux, read_data(kbdc));
395 DELAY(KBDC_DELAYTIME);
403 * wait for any data; whether it's from the controller,
404 * the keyboard, or the aux device.
407 wait_for_data(struct atkbdc_softc *kbdc)
409 /* CPU will stay inside the loop for 200msec at most */
413 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
414 DELAY(KBDC_DELAYTIME);
418 DELAY(KBDD_DELAYTIME);
422 /* wait for data from the keyboard */
424 wait_for_kbd_data(struct atkbdc_softc *kbdc)
426 /* CPU will stay inside the loop for 200msec at most */
430 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
431 != KBDS_KBD_BUFFER_FULL) {
432 if (f == KBDS_AUX_BUFFER_FULL) {
433 DELAY(KBDD_DELAYTIME);
434 addq(&kbdc->aux, read_data(kbdc));
436 DELAY(KBDC_DELAYTIME);
440 DELAY(KBDD_DELAYTIME);
445 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
446 * queue anything else.
449 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
451 /* CPU will stay inside the loop for 200msec at most */
456 while (retry-- > 0) {
457 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
458 DELAY(KBDD_DELAYTIME);
460 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
461 if ((b == KBD_ACK) || (b == KBD_RESEND)
462 || (b == KBD_RESET_FAIL))
465 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
469 DELAY(KBDC_DELAYTIME);
474 /* wait for data from the aux device */
476 wait_for_aux_data(struct atkbdc_softc *kbdc)
478 /* CPU will stay inside the loop for 200msec at most */
482 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
483 != KBDS_AUX_BUFFER_FULL) {
484 if (f == KBDS_KBD_BUFFER_FULL) {
485 DELAY(KBDD_DELAYTIME);
486 addq(&kbdc->kbd, read_data(kbdc));
488 DELAY(KBDC_DELAYTIME);
492 DELAY(KBDD_DELAYTIME);
497 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
498 * queue anything else.
501 wait_for_aux_ack(struct atkbdc_softc *kbdc)
503 /* CPU will stay inside the loop for 200msec at most */
508 while (retry-- > 0) {
509 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
510 DELAY(KBDD_DELAYTIME);
512 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
513 if ((b == PSM_ACK) || (b == PSM_RESEND)
514 || (b == PSM_RESET_FAIL))
517 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
521 DELAY(KBDC_DELAYTIME);
526 /* write a one byte command to the controller */
528 write_controller_command(KBDC p, int c)
530 if (!wait_while_controller_busy(kbdcp(p)))
532 write_command(kbdcp(p), c);
536 /* write a one byte data to the controller */
538 write_controller_data(KBDC p, int c)
540 if (!wait_while_controller_busy(kbdcp(p)))
542 write_data(kbdcp(p), c);
546 /* write a one byte keyboard command */
548 write_kbd_command(KBDC p, int c)
550 if (!wait_while_controller_busy(kbdcp(p)))
552 write_data(kbdcp(p), c);
556 /* write a one byte auxiliary device command */
558 write_aux_command(KBDC p, int c)
560 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
562 return write_controller_data(p, c);
565 /* send a command to the keyboard and wait for ACK */
567 send_kbd_command(KBDC p, int c)
569 int retry = KBD_MAXRETRY;
572 while (retry-- > 0) {
573 if (!write_kbd_command(p, c))
575 res = wait_for_kbd_ack(kbdcp(p));
582 /* send a command to the auxiliary device and wait for ACK */
584 send_aux_command(KBDC p, int c)
586 int retry = KBD_MAXRETRY;
589 while (retry-- > 0) {
590 if (!write_aux_command(p, c))
594 * The aux device may have already sent one or two bytes of
595 * status data, when a command is received. It will immediately
596 * stop data transmission, thus, leaving an incomplete data
597 * packet in our buffer. We have to discard any unprocessed
598 * data in order to remove such packets. Well, we may remove
599 * unprocessed, but necessary data byte as well...
601 emptyq(&kbdcp(p)->aux);
602 res = wait_for_aux_ack(kbdcp(p));
609 /* send a command and a data to the keyboard, wait for ACKs */
611 send_kbd_command_and_data(KBDC p, int c, int d)
616 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
617 if (!write_kbd_command(p, c))
619 res = wait_for_kbd_ack(kbdcp(p));
622 else if (res != KBD_RESEND)
628 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
629 if (!write_kbd_command(p, d))
631 res = wait_for_kbd_ack(kbdcp(p));
632 if (res != KBD_RESEND)
638 /* send a command and a data to the auxiliary device, wait for ACKs */
640 send_aux_command_and_data(KBDC p, int c, int d)
645 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
646 if (!write_aux_command(p, c))
648 emptyq(&kbdcp(p)->aux);
649 res = wait_for_aux_ack(kbdcp(p));
652 else if (res != PSM_RESEND)
658 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
659 if (!write_aux_command(p, d))
661 res = wait_for_aux_ack(kbdcp(p));
662 if (res != PSM_RESEND)
669 * read one byte from any source; whether from the controller,
670 * the keyboard, or the aux device
673 read_controller_data(KBDC p)
675 if (availq(&kbdcp(p)->kbd))
676 return removeq(&kbdcp(p)->kbd);
677 if (availq(&kbdcp(p)->aux))
678 return removeq(&kbdcp(p)->aux);
679 if (!wait_for_data(kbdcp(p)))
680 return -1; /* timeout */
681 return read_data(kbdcp(p));
688 /* read one byte from the keyboard */
690 read_kbd_data(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 if (!wait_for_kbd_data(kbdcp(p)))
705 return -1; /* timeout */
706 return read_data(kbdcp(p));
709 /* read one byte from the keyboard, but return immediately if
713 read_kbd_data_no_wait(KBDC p)
720 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
721 "aux q: %d calls, max %d chars\n",
722 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
723 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
727 if (availq(&kbdcp(p)->kbd))
728 return removeq(&kbdcp(p)->kbd);
729 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
730 if (f == KBDS_AUX_BUFFER_FULL) {
731 DELAY(KBDD_DELAYTIME);
732 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
733 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
735 if (f == KBDS_KBD_BUFFER_FULL) {
736 DELAY(KBDD_DELAYTIME);
737 return read_data(kbdcp(p));
739 return -1; /* no data */
742 /* read one byte from the aux device */
744 read_aux_data(KBDC p)
746 if (availq(&kbdcp(p)->aux))
747 return removeq(&kbdcp(p)->aux);
748 if (!wait_for_aux_data(kbdcp(p)))
749 return -1; /* timeout */
750 return read_data(kbdcp(p));
753 /* read one byte from the aux device, but return immediately if
757 read_aux_data_no_wait(KBDC p)
761 if (availq(&kbdcp(p)->aux))
762 return removeq(&kbdcp(p)->aux);
763 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
764 if (f == KBDS_KBD_BUFFER_FULL) {
765 DELAY(KBDD_DELAYTIME);
766 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
767 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
769 if (f == KBDS_AUX_BUFFER_FULL) {
770 DELAY(KBDD_DELAYTIME);
771 return read_data(kbdcp(p));
773 return -1; /* no data */
776 /* discard data from the keyboard */
778 empty_kbd_buffer(KBDC p, int wait)
789 for (t = wait; t > 0; ) {
790 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
791 DELAY(KBDD_DELAYTIME);
792 b = read_data(kbdcp(p));
793 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
794 addq(&kbdcp(p)->aux, b);
808 if ((c1 > 0) || (c2 > 0))
809 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
812 emptyq(&kbdcp(p)->kbd);
815 /* discard data from the aux device */
817 empty_aux_buffer(KBDC p, int wait)
828 for (t = wait; t > 0; ) {
829 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
830 DELAY(KBDD_DELAYTIME);
831 b = read_data(kbdcp(p));
832 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
833 addq(&kbdcp(p)->kbd, b);
847 if ((c1 > 0) || (c2 > 0))
848 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
851 emptyq(&kbdcp(p)->aux);
854 /* discard any data from the keyboard or the aux device */
856 empty_both_buffers(KBDC p, int wait)
867 for (t = wait; t > 0; ) {
868 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
869 DELAY(KBDD_DELAYTIME);
870 (void)read_data(kbdcp(p));
872 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
883 * Some systems (Intel/IBM blades) do not have keyboard devices and
884 * will thus hang in this procedure. Time out after delta seconds to
885 * avoid this hang -- the keyboard attach will fail later on.
887 waited += (delta * 1000);
888 if (waited == (delta * 1000000))
894 if ((c1 > 0) || (c2 > 0))
895 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
898 emptyq(&kbdcp(p)->kbd);
899 emptyq(&kbdcp(p)->aux);
902 /* keyboard and mouse device control */
904 /* NOTE: enable the keyboard port but disable the keyboard
905 * interrupt before calling "reset_kbd()".
910 int retry = KBD_MAXRETRY;
911 int again = KBD_MAXWAIT;
912 int c = KBD_RESEND; /* keep the compiler happy */
914 while (retry-- > 0) {
915 empty_both_buffers(p, 10);
916 if (!write_kbd_command(p, KBDC_RESET_KBD))
918 emptyq(&kbdcp(p)->kbd);
919 c = read_controller_data(p);
920 if (verbose || bootverbose)
921 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
922 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
928 while (again-- > 0) {
929 /* wait awhile, well, in fact we must wait quite loooooooooooong */
930 DELAY(KBD_RESETDELAY*1000);
931 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
932 if (c != -1) /* wait again if the controller is not ready */
935 if (verbose || bootverbose)
936 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
937 if (c != KBD_RESET_DONE)
942 /* NOTE: enable the aux port but disable the aux interrupt
943 * before calling `reset_aux_dev()'.
946 reset_aux_dev(KBDC p)
948 int retry = KBD_MAXRETRY;
949 int again = KBD_MAXWAIT;
950 int c = PSM_RESEND; /* keep the compiler happy */
952 while (retry-- > 0) {
953 empty_both_buffers(p, 10);
954 if (!write_aux_command(p, PSMC_RESET_DEV))
956 emptyq(&kbdcp(p)->aux);
957 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
958 for (again = KBD_MAXWAIT; again > 0; --again) {
959 DELAY(KBD_RESETDELAY*1000);
960 c = read_aux_data_no_wait(p);
964 if (verbose || bootverbose)
965 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
966 if (c == PSM_ACK) /* aux dev is about to reset... */
972 for (again = KBD_MAXWAIT; again > 0; --again) {
973 /* wait awhile, well, quite looooooooooooong */
974 DELAY(KBD_RESETDELAY*1000);
975 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
976 if (c != -1) /* wait again if the controller is not ready */
979 if (verbose || bootverbose)
980 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
981 if (c != PSM_RESET_DONE) /* reset status */
984 c = read_aux_data(p); /* device ID */
985 if (verbose || bootverbose)
986 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
987 /* NOTE: we could check the device ID now, but leave it later... */
991 /* controller diagnostics and setup */
994 test_controller(KBDC p)
996 int retry = KBD_MAXRETRY;
997 int again = KBD_MAXWAIT;
998 int c = KBD_DIAG_FAIL;
1000 while (retry-- > 0) {
1001 empty_both_buffers(p, 10);
1002 if (write_controller_command(p, KBDC_DIAGNOSE))
1008 emptyq(&kbdcp(p)->kbd);
1009 while (again-- > 0) {
1011 DELAY(KBD_RESETDELAY*1000);
1012 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
1013 if (c != -1) /* wait again if the controller is not ready */
1016 if (verbose || bootverbose)
1017 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
1018 return (c == KBD_DIAG_DONE);
1022 test_kbd_port(KBDC p)
1024 int retry = KBD_MAXRETRY;
1025 int again = KBD_MAXWAIT;
1028 while (retry-- > 0) {
1029 empty_both_buffers(p, 10);
1030 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1036 emptyq(&kbdcp(p)->kbd);
1037 while (again-- > 0) {
1038 c = read_controller_data(p);
1039 if (c != -1) /* try again if the controller is not ready */
1042 if (verbose || bootverbose)
1043 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1048 test_aux_port(KBDC p)
1050 int retry = KBD_MAXRETRY;
1051 int again = KBD_MAXWAIT;
1054 while (retry-- > 0) {
1055 empty_both_buffers(p, 10);
1056 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1062 emptyq(&kbdcp(p)->kbd);
1063 while (again-- > 0) {
1064 c = read_controller_data(p);
1065 if (c != -1) /* try again if the controller is not ready */
1068 if (verbose || bootverbose)
1069 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1074 kbdc_get_device_mask(KBDC p)
1076 return kbdcp(p)->command_mask;
1080 kbdc_set_device_mask(KBDC p, int mask)
1082 kbdcp(p)->command_mask =
1083 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1087 get_controller_command_byte(KBDC p)
1089 if (kbdcp(p)->command_byte != -1)
1090 return kbdcp(p)->command_byte;
1091 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1093 emptyq(&kbdcp(p)->kbd);
1094 kbdcp(p)->command_byte = read_controller_data(p);
1095 return kbdcp(p)->command_byte;
1099 set_controller_command_byte(KBDC p, int mask, int command)
1101 if (get_controller_command_byte(p) == -1)
1104 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1105 if (command & KBD_DISABLE_KBD_PORT) {
1106 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1109 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1111 if (!write_controller_data(p, command))
1113 kbdcp(p)->command_byte = command;
1116 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",