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 #if defined(__amd64__)
48 #include <machine/clock.h>
51 #include <dev/atkbdc/atkbdcreg.h>
54 #include <dev/ofw/openfirm.h>
55 #include <machine/bus_private.h>
56 #include <machine/ofw_machdep.h>
58 #include <isa/isareg.h>
63 #define MAXKBDC 1 /* XXX */
68 #define MAX(x, y) ((x) > (y) ? (x) : (y))
71 #define kbdcp(p) ((atkbdc_softc_t *)(p))
72 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
73 #define availq(q) ((q)->head != (q)->tail)
75 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
77 #define emptyq(q) ((q)->tail = (q)->head = 0)
80 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
81 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
82 #define write_data(k, d) \
83 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
84 #define write_command(k, d) \
85 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
90 * We always need at least one copy of the kbdc_softc struct for the
91 * low-level console. As the low-level console accesses the keyboard
92 * controller before kbdc, and all other devices, is probed, we
93 * statically allocate one entry. XXX
95 static atkbdc_softc_t default_kbdc;
96 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
98 static int verbose = KBDIO_DEBUG;
101 static struct bus_space_tag atkbdc_bst_store[MAXKBDC];
104 /* function prototypes */
106 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
107 bus_space_handle_t h0, bus_space_handle_t h1);
108 static int addq(kqueue *q, int c);
109 static int removeq(kqueue *q);
110 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
111 static int wait_for_data(atkbdc_softc_t *kbdc);
112 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
113 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
114 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
115 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
118 *atkbdc_get_softc(int unit)
122 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
124 sc = atkbdc_softc[unit];
126 sc = atkbdc_softc[unit]
127 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO);
135 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
137 if (rman_get_start(port0) <= 0)
139 if (rman_get_start(port1) <= 0)
145 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
146 struct resource *port1)
148 return atkbdc_setup(sc, rman_get_bustag(port0),
149 rman_get_bushandle(port0),
150 rman_get_bushandle(port1));
153 /* the backdoor to the keyboard controller! XXX */
155 atkbdc_configure(void)
158 bus_space_handle_t h0;
159 bus_space_handle_t h1;
160 #if defined(__i386__) || defined(__amd64__)
166 phandle_t chosen, node;
176 /* XXX: tag should be passed from the caller */
177 #if defined(__amd64__) || defined(__i386__)
178 tag = X86_BUS_SPACE_IO;
179 #elif defined(__ia64__)
180 tag = IA64_BUS_SPACE_IO;
181 #elif defined(__sparc64__)
182 tag = &atkbdc_bst_store[0];
188 if ((chosen = OF_finddevice("/chosen")) == -1)
190 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
192 if ((node = OF_instance_to_package(stdin)) == -1)
194 if (OF_getprop(node, "name", name, sizeof(name)) == -1)
196 name[sizeof(name) - 1] = '\0';
197 if (strcmp(name, "kb_ps2") != 0)
200 * The stdin handle points to an instance of a PS/2 keyboard
201 * package but we want the 8042 controller, which is the parent
202 * of that keyboard node.
204 if ((node = OF_parent(node)) == 0)
206 if (OF_decode_addr(node, 0, &space, &port0) != 0)
208 h0 = sparc64_fake_bustag(space, port0, tag);
209 bus_space_subregion(tag, h0, KBD_DATA_PORT, 1, &h0);
210 if (OF_decode_addr(node, 1, &space, &port1) != 0)
212 h1 = sparc64_fake_bustag(space, port1, tag);
213 bus_space_subregion(tag, h1, KBD_STATUS_PORT, 1, &h1);
216 resource_int_value("atkbdc", 0, "port", &port0);
217 port1 = IO_KBD + KBD_STATUS_PORT;
219 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
220 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
222 h0 = (bus_space_handle_t)port0;
223 h1 = (bus_space_handle_t)port1;
227 #if defined(__i386__) || defined(__amd64__)
229 * Check if we really have AT keyboard controller. Poll status
230 * register until we get "all clear" indication. If no such
231 * indication comes, it probably means that there is no AT
232 * keyboard controller present. Give up in such case. Check relies
233 * on the fact that reading from non-existing in/out port returns
234 * 0xff on i386. May or may not be true on other platforms.
236 flags = intr_disable();
237 for (i = 0; i != 65535; i++) {
238 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
246 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
250 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
251 bus_space_handle_t h1)
253 #if defined(__amd64__)
254 u_int64_t tscval[3], read_delay;
258 if (sc->ioh0 == 0) { /* XXX */
259 sc->command_byte = -1;
260 sc->command_mask = 0;
262 sc->kbd.head = sc->kbd.tail = 0;
263 sc->aux.head = sc->aux.tail = 0;
265 sc->kbd.call_count = 0;
266 sc->kbd.qcount = sc->kbd.max_qcount = 0;
267 sc->aux.call_count = 0;
268 sc->aux.qcount = sc->aux.max_qcount = 0;
275 #if defined(__amd64__)
277 * On certain chipsets AT keyboard controller isn't present and is
278 * emulated by BIOS using SMI interrupt. On those chipsets reading
279 * from the status port may be thousand times slower than usually.
280 * Sometimes this emilation is not working properly resulting in
281 * commands timing our and since we assume that inb() operation
282 * takes very little time to complete we need to adjust number of
283 * retries to keep waiting time within a designed limits (100ms).
284 * Measure time it takes to make read_status() call and adjust
285 * number of retries accordingly.
287 flags = intr_disable();
294 read_delay = tscval[1] - tscval[0];
295 read_delay /= (tscval[2] - tscval[1]) / 1000;
296 sc->retry = 100000 / ((KBDD_DELAYTIME * 2) + read_delay);
304 /* open a keyboard controller */
306 atkbdc_open(int unit)
312 if ((atkbdc_softc[unit]->port0 != NULL)
313 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
314 return (KBDC)atkbdc_softc[unit];
319 * I/O access arbitration in `kbdio'
321 * The `kbdio' module uses a simplistic convention to arbitrate
322 * I/O access to the controller/keyboard/mouse. The convention requires
323 * close cooperation of the calling device driver.
325 * The device drivers which utilize the `kbdio' module are assumed to
326 * have the following set of routines.
327 * a. An interrupt handler (the bottom half of the driver).
328 * b. Timeout routines which may briefly poll the keyboard controller.
329 * c. Routines outside interrupt context (the top half of the driver).
330 * They should follow the rules below:
331 * 1. The interrupt handler may assume that it always has full access
332 * to the controller/keyboard/mouse.
333 * 2. The other routines must issue `spltty()' if they wish to
334 * prevent the interrupt handler from accessing
335 * the controller/keyboard/mouse.
336 * 3. The timeout routines and the top half routines of the device driver
337 * arbitrate I/O access by observing the lock flag in `kbdio'.
338 * The flag is manipulated via `kbdc_lock()'; when one wants to
339 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
340 * the call returns with TRUE. Otherwise the caller must back off.
341 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
342 * is finished. This mechanism does not prevent the interrupt
343 * handler from being invoked at any time and carrying out I/O.
344 * Therefore, `spltty()' must be strategically placed in the device
345 * driver code. Also note that the timeout routine may interrupt
346 * `kbdc_lock()' called by the top half of the driver, but this
347 * interruption is OK so long as the timeout routine observes
349 * 4. The interrupt and timeout routines should not extend I/O operation
350 * across more than one interrupt or timeout; they must complete any
351 * necessary I/O operation within one invocation of the routine.
352 * This means that if the timeout routine acquires the lock flag,
353 * it must reset the flag to FALSE before it returns.
356 /* set/reset polling lock */
358 kbdc_lock(KBDC p, int lock)
362 prevlock = kbdcp(p)->lock;
363 kbdcp(p)->lock = lock;
365 return (prevlock != lock);
368 /* check if any data is waiting to be processed */
370 kbdc_data_ready(KBDC p)
372 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
373 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
376 /* queuing functions */
379 addq(kqueue *q, int c)
381 if (nextq(q->tail) != q->head) {
383 q->tail = nextq(q->tail);
387 if (q->qcount > q->max_qcount)
388 q->max_qcount = q->qcount;
400 if (q->tail != q->head) {
402 q->head = nextq(q->head);
412 * device I/O routines
415 wait_while_controller_busy(struct atkbdc_softc *kbdc)
420 /* CPU will stay inside the loop for 100msec at most */
423 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
424 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
425 DELAY(KBDD_DELAYTIME);
426 addq(&kbdc->kbd, read_data(kbdc));
427 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
428 DELAY(KBDD_DELAYTIME);
429 addq(&kbdc->aux, read_data(kbdc));
431 DELAY(KBDC_DELAYTIME);
439 * wait for any data; whether it's from the controller,
440 * the keyboard, or the aux device.
443 wait_for_data(struct atkbdc_softc *kbdc)
448 /* CPU will stay inside the loop for 200msec at most */
449 retry = kbdc->retry * 2;
451 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
452 DELAY(KBDC_DELAYTIME);
456 DELAY(KBDD_DELAYTIME);
460 /* wait for data from the keyboard */
462 wait_for_kbd_data(struct atkbdc_softc *kbdc)
467 /* CPU will stay inside the loop for 200msec at most */
468 retry = kbdc->retry * 2;
470 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
471 != KBDS_KBD_BUFFER_FULL) {
472 if (f == KBDS_AUX_BUFFER_FULL) {
473 DELAY(KBDD_DELAYTIME);
474 addq(&kbdc->aux, read_data(kbdc));
476 DELAY(KBDC_DELAYTIME);
480 DELAY(KBDD_DELAYTIME);
485 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
486 * queue anything else.
489 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
495 /* CPU will stay inside the loop for 200msec at most */
496 retry = kbdc->retry * 2;
498 while (retry-- > 0) {
499 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
500 DELAY(KBDD_DELAYTIME);
502 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
503 if ((b == KBD_ACK) || (b == KBD_RESEND)
504 || (b == KBD_RESET_FAIL))
507 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
511 DELAY(KBDC_DELAYTIME);
516 /* wait for data from the aux device */
518 wait_for_aux_data(struct atkbdc_softc *kbdc)
523 /* CPU will stay inside the loop for 200msec at most */
524 retry = kbdc->retry * 2;
526 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
527 != KBDS_AUX_BUFFER_FULL) {
528 if (f == KBDS_KBD_BUFFER_FULL) {
529 DELAY(KBDD_DELAYTIME);
530 addq(&kbdc->kbd, read_data(kbdc));
532 DELAY(KBDC_DELAYTIME);
536 DELAY(KBDD_DELAYTIME);
541 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
542 * queue anything else.
545 wait_for_aux_ack(struct atkbdc_softc *kbdc)
551 /* CPU will stay inside the loop for 200msec at most */
552 retry = kbdc->retry * 2;
554 while (retry-- > 0) {
555 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
556 DELAY(KBDD_DELAYTIME);
558 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
559 if ((b == PSM_ACK) || (b == PSM_RESEND)
560 || (b == PSM_RESET_FAIL))
563 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
567 DELAY(KBDC_DELAYTIME);
572 /* write a one byte command to the controller */
574 write_controller_command(KBDC p, int c)
576 if (!wait_while_controller_busy(kbdcp(p)))
578 write_command(kbdcp(p), c);
582 /* write a one byte data to the controller */
584 write_controller_data(KBDC p, int c)
586 if (!wait_while_controller_busy(kbdcp(p)))
588 write_data(kbdcp(p), c);
592 /* write a one byte keyboard command */
594 write_kbd_command(KBDC p, int c)
596 if (!wait_while_controller_busy(kbdcp(p)))
598 write_data(kbdcp(p), c);
602 /* write a one byte auxiliary device command */
604 write_aux_command(KBDC p, int c)
606 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
608 return write_controller_data(p, c);
611 /* send a command to the keyboard and wait for ACK */
613 send_kbd_command(KBDC p, int c)
615 int retry = KBD_MAXRETRY;
618 while (retry-- > 0) {
619 if (!write_kbd_command(p, c))
621 res = wait_for_kbd_ack(kbdcp(p));
628 /* send a command to the auxiliary device and wait for ACK */
630 send_aux_command(KBDC p, int c)
632 int retry = KBD_MAXRETRY;
635 while (retry-- > 0) {
636 if (!write_aux_command(p, c))
640 * The aux device may have already sent one or two bytes of
641 * status data, when a command is received. It will immediately
642 * stop data transmission, thus, leaving an incomplete data
643 * packet in our buffer. We have to discard any unprocessed
644 * data in order to remove such packets. Well, we may remove
645 * unprocessed, but necessary data byte as well...
647 emptyq(&kbdcp(p)->aux);
648 res = wait_for_aux_ack(kbdcp(p));
655 /* send a command and a data to the keyboard, wait for ACKs */
657 send_kbd_command_and_data(KBDC p, int c, int d)
662 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
663 if (!write_kbd_command(p, c))
665 res = wait_for_kbd_ack(kbdcp(p));
668 else if (res != KBD_RESEND)
674 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
675 if (!write_kbd_command(p, d))
677 res = wait_for_kbd_ack(kbdcp(p));
678 if (res != KBD_RESEND)
684 /* send a command and a data to the auxiliary device, wait for ACKs */
686 send_aux_command_and_data(KBDC p, int c, int d)
691 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
692 if (!write_aux_command(p, c))
694 emptyq(&kbdcp(p)->aux);
695 res = wait_for_aux_ack(kbdcp(p));
698 else if (res != PSM_RESEND)
704 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
705 if (!write_aux_command(p, d))
707 res = wait_for_aux_ack(kbdcp(p));
708 if (res != PSM_RESEND)
715 * read one byte from any source; whether from the controller,
716 * the keyboard, or the aux device
719 read_controller_data(KBDC p)
721 if (availq(&kbdcp(p)->kbd))
722 return removeq(&kbdcp(p)->kbd);
723 if (availq(&kbdcp(p)->aux))
724 return removeq(&kbdcp(p)->aux);
725 if (!wait_for_data(kbdcp(p)))
726 return -1; /* timeout */
727 return read_data(kbdcp(p));
734 /* read one byte from the keyboard */
736 read_kbd_data(KBDC p)
741 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
742 "aux q: %d calls, max %d chars\n",
743 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
744 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
748 if (availq(&kbdcp(p)->kbd))
749 return removeq(&kbdcp(p)->kbd);
750 if (!wait_for_kbd_data(kbdcp(p)))
751 return -1; /* timeout */
752 return read_data(kbdcp(p));
755 /* read one byte from the keyboard, but return immediately if
759 read_kbd_data_no_wait(KBDC p)
766 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
767 "aux q: %d calls, max %d chars\n",
768 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
769 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
773 if (availq(&kbdcp(p)->kbd))
774 return removeq(&kbdcp(p)->kbd);
775 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
776 if (f == KBDS_AUX_BUFFER_FULL) {
777 DELAY(KBDD_DELAYTIME);
778 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
779 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
781 if (f == KBDS_KBD_BUFFER_FULL) {
782 DELAY(KBDD_DELAYTIME);
783 return read_data(kbdcp(p));
785 return -1; /* no data */
788 /* read one byte from the aux device */
790 read_aux_data(KBDC p)
792 if (availq(&kbdcp(p)->aux))
793 return removeq(&kbdcp(p)->aux);
794 if (!wait_for_aux_data(kbdcp(p)))
795 return -1; /* timeout */
796 return read_data(kbdcp(p));
799 /* read one byte from the aux device, but return immediately if
803 read_aux_data_no_wait(KBDC p)
807 if (availq(&kbdcp(p)->aux))
808 return removeq(&kbdcp(p)->aux);
809 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
810 if (f == KBDS_KBD_BUFFER_FULL) {
811 DELAY(KBDD_DELAYTIME);
812 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
813 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
815 if (f == KBDS_AUX_BUFFER_FULL) {
816 DELAY(KBDD_DELAYTIME);
817 return read_data(kbdcp(p));
819 return -1; /* no data */
822 /* discard data from the keyboard */
824 empty_kbd_buffer(KBDC p, int wait)
835 for (t = wait; t > 0; ) {
836 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
837 DELAY(KBDD_DELAYTIME);
838 b = read_data(kbdcp(p));
839 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
840 addq(&kbdcp(p)->aux, b);
854 if ((c1 > 0) || (c2 > 0))
855 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
858 emptyq(&kbdcp(p)->kbd);
861 /* discard data from the aux device */
863 empty_aux_buffer(KBDC p, int wait)
874 for (t = wait; t > 0; ) {
875 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
876 DELAY(KBDD_DELAYTIME);
877 b = read_data(kbdcp(p));
878 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
879 addq(&kbdcp(p)->kbd, b);
893 if ((c1 > 0) || (c2 > 0))
894 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
897 emptyq(&kbdcp(p)->aux);
900 /* discard any data from the keyboard or the aux device */
902 empty_both_buffers(KBDC p, int wait)
913 for (t = wait; t > 0; ) {
914 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
915 DELAY(KBDD_DELAYTIME);
916 (void)read_data(kbdcp(p));
918 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
929 * Some systems (Intel/IBM blades) do not have keyboard devices and
930 * will thus hang in this procedure. Time out after delta seconds to
931 * avoid this hang -- the keyboard attach will fail later on.
933 waited += (delta * 1000);
934 if (waited == (delta * 1000000))
940 if ((c1 > 0) || (c2 > 0))
941 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
944 emptyq(&kbdcp(p)->kbd);
945 emptyq(&kbdcp(p)->aux);
948 /* keyboard and mouse device control */
950 /* NOTE: enable the keyboard port but disable the keyboard
951 * interrupt before calling "reset_kbd()".
956 int retry = KBD_MAXRETRY;
957 int again = KBD_MAXWAIT;
958 int c = KBD_RESEND; /* keep the compiler happy */
960 while (retry-- > 0) {
961 empty_both_buffers(p, 10);
962 if (!write_kbd_command(p, KBDC_RESET_KBD))
964 emptyq(&kbdcp(p)->kbd);
965 c = read_controller_data(p);
966 if (verbose || bootverbose)
967 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
968 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
974 while (again-- > 0) {
975 /* wait awhile, well, in fact we must wait quite loooooooooooong */
976 DELAY(KBD_RESETDELAY*1000);
977 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
978 if (c != -1) /* wait again if the controller is not ready */
981 if (verbose || bootverbose)
982 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
983 if (c != KBD_RESET_DONE)
988 /* NOTE: enable the aux port but disable the aux interrupt
989 * before calling `reset_aux_dev()'.
992 reset_aux_dev(KBDC p)
994 int retry = KBD_MAXRETRY;
995 int again = KBD_MAXWAIT;
996 int c = PSM_RESEND; /* keep the compiler happy */
998 while (retry-- > 0) {
999 empty_both_buffers(p, 10);
1000 if (!write_aux_command(p, PSMC_RESET_DEV))
1002 emptyq(&kbdcp(p)->aux);
1003 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
1004 for (again = KBD_MAXWAIT; again > 0; --again) {
1005 DELAY(KBD_RESETDELAY*1000);
1006 c = read_aux_data_no_wait(p);
1010 if (verbose || bootverbose)
1011 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
1012 if (c == PSM_ACK) /* aux dev is about to reset... */
1018 for (again = KBD_MAXWAIT; again > 0; --again) {
1019 /* wait awhile, well, quite looooooooooooong */
1020 DELAY(KBD_RESETDELAY*1000);
1021 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
1022 if (c != -1) /* wait again if the controller is not ready */
1025 if (verbose || bootverbose)
1026 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
1027 if (c != PSM_RESET_DONE) /* reset status */
1030 c = read_aux_data(p); /* device ID */
1031 if (verbose || bootverbose)
1032 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
1033 /* NOTE: we could check the device ID now, but leave it later... */
1037 /* controller diagnostics and setup */
1040 test_controller(KBDC p)
1042 int retry = KBD_MAXRETRY;
1043 int again = KBD_MAXWAIT;
1044 int c = KBD_DIAG_FAIL;
1046 while (retry-- > 0) {
1047 empty_both_buffers(p, 10);
1048 if (write_controller_command(p, KBDC_DIAGNOSE))
1054 emptyq(&kbdcp(p)->kbd);
1055 while (again-- > 0) {
1057 DELAY(KBD_RESETDELAY*1000);
1058 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
1059 if (c != -1) /* wait again if the controller is not ready */
1062 if (verbose || bootverbose)
1063 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
1064 return (c == KBD_DIAG_DONE);
1068 test_kbd_port(KBDC p)
1070 int retry = KBD_MAXRETRY;
1071 int again = KBD_MAXWAIT;
1074 while (retry-- > 0) {
1075 empty_both_buffers(p, 10);
1076 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1082 emptyq(&kbdcp(p)->kbd);
1083 while (again-- > 0) {
1084 c = read_controller_data(p);
1085 if (c != -1) /* try again if the controller is not ready */
1088 if (verbose || bootverbose)
1089 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1094 test_aux_port(KBDC p)
1096 int retry = KBD_MAXRETRY;
1097 int again = KBD_MAXWAIT;
1100 while (retry-- > 0) {
1101 empty_both_buffers(p, 10);
1102 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1108 emptyq(&kbdcp(p)->kbd);
1109 while (again-- > 0) {
1110 c = read_controller_data(p);
1111 if (c != -1) /* try again if the controller is not ready */
1114 if (verbose || bootverbose)
1115 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1120 kbdc_get_device_mask(KBDC p)
1122 return kbdcp(p)->command_mask;
1126 kbdc_set_device_mask(KBDC p, int mask)
1128 kbdcp(p)->command_mask =
1129 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1133 get_controller_command_byte(KBDC p)
1135 if (kbdcp(p)->command_byte != -1)
1136 return kbdcp(p)->command_byte;
1137 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1139 emptyq(&kbdcp(p)->kbd);
1140 kbdcp(p)->command_byte = read_controller_data(p);
1141 return kbdcp(p)->command_byte;
1145 set_controller_command_byte(KBDC p, int mask, int command)
1147 if (get_controller_command_byte(p) == -1)
1150 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1151 if (command & KBD_DISABLE_KBD_PORT) {
1152 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1155 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1157 if (!write_controller_data(p, command))
1159 kbdcp(p)->command_byte = command;
1162 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",