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;
158 phandle_t chosen, node;
168 /* XXX: tag should be passed from the caller */
169 #if defined(__i386__)
170 tag = I386_BUS_SPACE_IO;
171 #elif defined(__amd64__)
172 tag = AMD64_BUS_SPACE_IO;
173 #elif defined(__alpha__)
174 tag = busspace_isa_io;
175 #elif defined(__ia64__)
176 tag = IA64_BUS_SPACE_IO;
177 #elif defined(__sparc64__)
178 tag = &atkbdc_bst_store[0];
184 if ((chosen = OF_finddevice("/chosen")) == -1)
186 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
188 if ((node = OF_instance_to_package(stdin)) == -1)
190 if (OF_getprop(node, "name", name, sizeof(name)) == -1)
192 name[sizeof(name) - 1] = '\0';
193 if (strcmp(name, "kb_ps2") != 0)
196 * The stdin handle points to an instance of a PS/2 keyboard
197 * package but we want the 8042 controller, which is the parent
198 * of that keyboard node.
200 if ((node = OF_parent(node)) == 0)
202 if (OF_decode_addr(node, 0, &space, &port0) != 0)
204 h0 = sparc64_fake_bustag(space, port0, tag);
205 bus_space_subregion(tag, h0, KBD_DATA_PORT, 1, &h0);
206 if (OF_decode_addr(node, 1, &space, &port1) != 0)
208 h1 = sparc64_fake_bustag(space, port1, tag);
209 bus_space_subregion(tag, h1, KBD_STATUS_PORT, 1, &h1);
212 resource_int_value("atkbdc", 0, "port", &port0);
213 port1 = IO_KBD + KBD_STATUS_PORT;
215 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
216 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
218 h0 = (bus_space_handle_t)port0;
219 h1 = (bus_space_handle_t)port1;
222 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
226 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
227 bus_space_handle_t h1)
229 if (sc->ioh0 == 0) { /* XXX */
230 sc->command_byte = -1;
231 sc->command_mask = 0;
233 sc->kbd.head = sc->kbd.tail = 0;
234 sc->aux.head = sc->aux.tail = 0;
236 sc->kbd.call_count = 0;
237 sc->kbd.qcount = sc->kbd.max_qcount = 0;
238 sc->aux.call_count = 0;
239 sc->aux.qcount = sc->aux.max_qcount = 0;
248 /* open a keyboard controller */
250 atkbdc_open(int unit)
256 if ((atkbdc_softc[unit]->port0 != NULL)
257 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
258 return (KBDC)atkbdc_softc[unit];
263 * I/O access arbitration in `kbdio'
265 * The `kbdio' module uses a simplistic convention to arbitrate
266 * I/O access to the controller/keyboard/mouse. The convention requires
267 * close cooperation of the calling device driver.
269 * The device drivers which utilize the `kbdio' module are assumed to
270 * have the following set of routines.
271 * a. An interrupt handler (the bottom half of the driver).
272 * b. Timeout routines which may briefly poll the keyboard controller.
273 * c. Routines outside interrupt context (the top half of the driver).
274 * They should follow the rules below:
275 * 1. The interrupt handler may assume that it always has full access
276 * to the controller/keyboard/mouse.
277 * 2. The other routines must issue `spltty()' if they wish to
278 * prevent the interrupt handler from accessing
279 * the controller/keyboard/mouse.
280 * 3. The timeout routines and the top half routines of the device driver
281 * arbitrate I/O access by observing the lock flag in `kbdio'.
282 * The flag is manipulated via `kbdc_lock()'; when one wants to
283 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
284 * the call returns with TRUE. Otherwise the caller must back off.
285 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
286 * is finished. This mechanism does not prevent the interrupt
287 * handler from being invoked at any time and carrying out I/O.
288 * Therefore, `spltty()' must be strategically placed in the device
289 * driver code. Also note that the timeout routine may interrupt
290 * `kbdc_lock()' called by the top half of the driver, but this
291 * interruption is OK so long as the timeout routine observes
293 * 4. The interrupt and timeout routines should not extend I/O operation
294 * across more than one interrupt or timeout; they must complete any
295 * necessary I/O operation within one invocation of the routine.
296 * This means that if the timeout routine acquires the lock flag,
297 * it must reset the flag to FALSE before it returns.
300 /* set/reset polling lock */
302 kbdc_lock(KBDC p, int lock)
306 prevlock = kbdcp(p)->lock;
307 kbdcp(p)->lock = lock;
309 return (prevlock != lock);
312 /* check if any data is waiting to be processed */
314 kbdc_data_ready(KBDC p)
316 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
317 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
320 /* queuing functions */
323 addq(kqueue *q, int c)
325 if (nextq(q->tail) != q->head) {
327 q->tail = nextq(q->tail);
331 if (q->qcount > q->max_qcount)
332 q->max_qcount = q->qcount;
344 if (q->tail != q->head) {
346 q->head = nextq(q->head);
356 * device I/O routines
359 wait_while_controller_busy(struct atkbdc_softc *kbdc)
361 /* CPU will stay inside the loop for 100msec at most */
365 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
366 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
367 DELAY(KBDD_DELAYTIME);
368 addq(&kbdc->kbd, read_data(kbdc));
369 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
370 DELAY(KBDD_DELAYTIME);
371 addq(&kbdc->aux, read_data(kbdc));
373 DELAY(KBDC_DELAYTIME);
381 * wait for any data; whether it's from the controller,
382 * the keyboard, or the aux device.
385 wait_for_data(struct atkbdc_softc *kbdc)
387 /* CPU will stay inside the loop for 200msec at most */
391 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
392 DELAY(KBDC_DELAYTIME);
396 DELAY(KBDD_DELAYTIME);
400 /* wait for data from the keyboard */
402 wait_for_kbd_data(struct atkbdc_softc *kbdc)
404 /* CPU will stay inside the loop for 200msec at most */
408 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
409 != KBDS_KBD_BUFFER_FULL) {
410 if (f == KBDS_AUX_BUFFER_FULL) {
411 DELAY(KBDD_DELAYTIME);
412 addq(&kbdc->aux, read_data(kbdc));
414 DELAY(KBDC_DELAYTIME);
418 DELAY(KBDD_DELAYTIME);
423 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
424 * queue anything else.
427 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
429 /* CPU will stay inside the loop for 200msec at most */
434 while (retry-- > 0) {
435 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
436 DELAY(KBDD_DELAYTIME);
438 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
439 if ((b == KBD_ACK) || (b == KBD_RESEND)
440 || (b == KBD_RESET_FAIL))
443 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
447 DELAY(KBDC_DELAYTIME);
452 /* wait for data from the aux device */
454 wait_for_aux_data(struct atkbdc_softc *kbdc)
456 /* CPU will stay inside the loop for 200msec at most */
460 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
461 != KBDS_AUX_BUFFER_FULL) {
462 if (f == KBDS_KBD_BUFFER_FULL) {
463 DELAY(KBDD_DELAYTIME);
464 addq(&kbdc->kbd, read_data(kbdc));
466 DELAY(KBDC_DELAYTIME);
470 DELAY(KBDD_DELAYTIME);
475 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
476 * queue anything else.
479 wait_for_aux_ack(struct atkbdc_softc *kbdc)
481 /* CPU will stay inside the loop for 200msec at most */
486 while (retry-- > 0) {
487 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
488 DELAY(KBDD_DELAYTIME);
490 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
491 if ((b == PSM_ACK) || (b == PSM_RESEND)
492 || (b == PSM_RESET_FAIL))
495 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
499 DELAY(KBDC_DELAYTIME);
504 /* write a one byte command to the controller */
506 write_controller_command(KBDC p, int c)
508 if (!wait_while_controller_busy(kbdcp(p)))
510 write_command(kbdcp(p), c);
514 /* write a one byte data to the controller */
516 write_controller_data(KBDC p, int c)
518 if (!wait_while_controller_busy(kbdcp(p)))
520 write_data(kbdcp(p), c);
524 /* write a one byte keyboard command */
526 write_kbd_command(KBDC p, int c)
528 if (!wait_while_controller_busy(kbdcp(p)))
530 write_data(kbdcp(p), c);
534 /* write a one byte auxiliary device command */
536 write_aux_command(KBDC p, int c)
538 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
540 return write_controller_data(p, c);
543 /* send a command to the keyboard and wait for ACK */
545 send_kbd_command(KBDC p, int c)
547 int retry = KBD_MAXRETRY;
550 while (retry-- > 0) {
551 if (!write_kbd_command(p, c))
553 res = wait_for_kbd_ack(kbdcp(p));
560 /* send a command to the auxiliary device and wait for ACK */
562 send_aux_command(KBDC p, int c)
564 int retry = KBD_MAXRETRY;
567 while (retry-- > 0) {
568 if (!write_aux_command(p, c))
572 * The aux device may have already sent one or two bytes of
573 * status data, when a command is received. It will immediately
574 * stop data transmission, thus, leaving an incomplete data
575 * packet in our buffer. We have to discard any unprocessed
576 * data in order to remove such packets. Well, we may remove
577 * unprocessed, but necessary data byte as well...
579 emptyq(&kbdcp(p)->aux);
580 res = wait_for_aux_ack(kbdcp(p));
587 /* send a command and a data to the keyboard, wait for ACKs */
589 send_kbd_command_and_data(KBDC p, int c, int d)
594 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
595 if (!write_kbd_command(p, c))
597 res = wait_for_kbd_ack(kbdcp(p));
600 else if (res != KBD_RESEND)
606 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
607 if (!write_kbd_command(p, d))
609 res = wait_for_kbd_ack(kbdcp(p));
610 if (res != KBD_RESEND)
616 /* send a command and a data to the auxiliary device, wait for ACKs */
618 send_aux_command_and_data(KBDC p, int c, int d)
623 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
624 if (!write_aux_command(p, c))
626 emptyq(&kbdcp(p)->aux);
627 res = wait_for_aux_ack(kbdcp(p));
630 else if (res != PSM_RESEND)
636 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
637 if (!write_aux_command(p, d))
639 res = wait_for_aux_ack(kbdcp(p));
640 if (res != PSM_RESEND)
647 * read one byte from any source; whether from the controller,
648 * the keyboard, or the aux device
651 read_controller_data(KBDC p)
653 if (availq(&kbdcp(p)->kbd))
654 return removeq(&kbdcp(p)->kbd);
655 if (availq(&kbdcp(p)->aux))
656 return removeq(&kbdcp(p)->aux);
657 if (!wait_for_data(kbdcp(p)))
658 return -1; /* timeout */
659 return read_data(kbdcp(p));
666 /* read one byte from the keyboard */
668 read_kbd_data(KBDC p)
673 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
674 "aux q: %d calls, max %d chars\n",
675 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
676 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
680 if (availq(&kbdcp(p)->kbd))
681 return removeq(&kbdcp(p)->kbd);
682 if (!wait_for_kbd_data(kbdcp(p)))
683 return -1; /* timeout */
684 return read_data(kbdcp(p));
687 /* read one byte from the keyboard, but return immediately if
691 read_kbd_data_no_wait(KBDC p)
698 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
699 "aux q: %d calls, max %d chars\n",
700 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
701 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
705 if (availq(&kbdcp(p)->kbd))
706 return removeq(&kbdcp(p)->kbd);
707 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
708 if (f == KBDS_AUX_BUFFER_FULL) {
709 DELAY(KBDD_DELAYTIME);
710 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
711 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
713 if (f == KBDS_KBD_BUFFER_FULL) {
714 DELAY(KBDD_DELAYTIME);
715 return read_data(kbdcp(p));
717 return -1; /* no data */
720 /* read one byte from the aux device */
722 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 return read_data(kbdcp(p));
731 /* read one byte from the aux device, but return immediately if
735 read_aux_data_no_wait(KBDC p)
739 if (availq(&kbdcp(p)->aux))
740 return removeq(&kbdcp(p)->aux);
741 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
742 if (f == KBDS_KBD_BUFFER_FULL) {
743 DELAY(KBDD_DELAYTIME);
744 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
745 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
747 if (f == KBDS_AUX_BUFFER_FULL) {
748 DELAY(KBDD_DELAYTIME);
749 return read_data(kbdcp(p));
751 return -1; /* no data */
754 /* discard data from the keyboard */
756 empty_kbd_buffer(KBDC p, int wait)
767 for (t = wait; t > 0; ) {
768 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
769 DELAY(KBDD_DELAYTIME);
770 b = read_data(kbdcp(p));
771 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
772 addq(&kbdcp(p)->aux, b);
786 if ((c1 > 0) || (c2 > 0))
787 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
790 emptyq(&kbdcp(p)->kbd);
793 /* discard data from the aux device */
795 empty_aux_buffer(KBDC p, int wait)
806 for (t = wait; t > 0; ) {
807 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
808 DELAY(KBDD_DELAYTIME);
809 b = read_data(kbdcp(p));
810 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
811 addq(&kbdcp(p)->kbd, b);
825 if ((c1 > 0) || (c2 > 0))
826 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
829 emptyq(&kbdcp(p)->aux);
832 /* discard any data from the keyboard or the aux device */
834 empty_both_buffers(KBDC p, int wait)
844 for (t = wait; t > 0; ) {
845 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
846 DELAY(KBDD_DELAYTIME);
847 (void)read_data(kbdcp(p));
849 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
861 if ((c1 > 0) || (c2 > 0))
862 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
865 emptyq(&kbdcp(p)->kbd);
866 emptyq(&kbdcp(p)->aux);
869 /* keyboard and mouse device control */
871 /* NOTE: enable the keyboard port but disable the keyboard
872 * interrupt before calling "reset_kbd()".
877 int retry = KBD_MAXRETRY;
878 int again = KBD_MAXWAIT;
879 int c = KBD_RESEND; /* keep the compiler happy */
881 while (retry-- > 0) {
882 empty_both_buffers(p, 10);
883 if (!write_kbd_command(p, KBDC_RESET_KBD))
885 emptyq(&kbdcp(p)->kbd);
886 c = read_controller_data(p);
887 if (verbose || bootverbose)
888 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
889 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
895 while (again-- > 0) {
896 /* wait awhile, well, in fact we must wait quite loooooooooooong */
897 DELAY(KBD_RESETDELAY*1000);
898 c = read_controller_data(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_KBD status:%04x\n", c);
904 if (c != KBD_RESET_DONE)
909 /* NOTE: enable the aux port but disable the aux interrupt
910 * before calling `reset_aux_dev()'.
913 reset_aux_dev(KBDC p)
915 int retry = KBD_MAXRETRY;
916 int again = KBD_MAXWAIT;
917 int c = PSM_RESEND; /* keep the compiler happy */
919 while (retry-- > 0) {
920 empty_both_buffers(p, 10);
921 if (!write_aux_command(p, PSMC_RESET_DEV))
923 emptyq(&kbdcp(p)->aux);
924 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
925 for (again = KBD_MAXWAIT; again > 0; --again) {
926 DELAY(KBD_RESETDELAY*1000);
927 c = read_aux_data_no_wait(p);
931 if (verbose || bootverbose)
932 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
933 if (c == PSM_ACK) /* aux dev is about to reset... */
939 for (again = KBD_MAXWAIT; again > 0; --again) {
940 /* wait awhile, well, quite looooooooooooong */
941 DELAY(KBD_RESETDELAY*1000);
942 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
943 if (c != -1) /* wait again if the controller is not ready */
946 if (verbose || bootverbose)
947 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
948 if (c != PSM_RESET_DONE) /* reset status */
951 c = read_aux_data(p); /* device ID */
952 if (verbose || bootverbose)
953 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
954 /* NOTE: we could check the device ID now, but leave it later... */
958 /* controller diagnostics and setup */
961 test_controller(KBDC p)
963 int retry = KBD_MAXRETRY;
964 int again = KBD_MAXWAIT;
965 int c = KBD_DIAG_FAIL;
967 while (retry-- > 0) {
968 empty_both_buffers(p, 10);
969 if (write_controller_command(p, KBDC_DIAGNOSE))
975 emptyq(&kbdcp(p)->kbd);
976 while (again-- > 0) {
978 DELAY(KBD_RESETDELAY*1000);
979 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
980 if (c != -1) /* wait again if the controller is not ready */
983 if (verbose || bootverbose)
984 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
985 return (c == KBD_DIAG_DONE);
989 test_kbd_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_KBD_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_KBD_PORT status:%04x\n", c);
1015 test_aux_port(KBDC p)
1017 int retry = KBD_MAXRETRY;
1018 int again = KBD_MAXWAIT;
1021 while (retry-- > 0) {
1022 empty_both_buffers(p, 10);
1023 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1029 emptyq(&kbdcp(p)->kbd);
1030 while (again-- > 0) {
1031 c = read_controller_data(p);
1032 if (c != -1) /* try again if the controller is not ready */
1035 if (verbose || bootverbose)
1036 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1041 kbdc_get_device_mask(KBDC p)
1043 return kbdcp(p)->command_mask;
1047 kbdc_set_device_mask(KBDC p, int mask)
1049 kbdcp(p)->command_mask =
1050 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1054 get_controller_command_byte(KBDC p)
1056 if (kbdcp(p)->command_byte != -1)
1057 return kbdcp(p)->command_byte;
1058 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1060 emptyq(&kbdcp(p)->kbd);
1061 kbdcp(p)->command_byte = read_controller_data(p);
1062 return kbdcp(p)->command_byte;
1066 set_controller_command_byte(KBDC p, int mask, int command)
1068 if (get_controller_command_byte(p) == -1)
1071 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1072 if (command & KBD_DISABLE_KBD_PORT) {
1073 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1076 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1078 if (!write_controller_data(p, command))
1080 kbdcp(p)->command_byte = command;
1083 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",