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(__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__) || defined(__amd64__)
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 defined(__amd64__)
252 u_int64_t tscval[3], read_delay;
256 if (sc->ioh0 == 0) { /* XXX */
257 sc->command_byte = -1;
258 sc->command_mask = 0;
260 sc->kbd.head = sc->kbd.tail = 0;
261 sc->aux.head = sc->aux.tail = 0;
263 sc->kbd.call_count = 0;
264 sc->kbd.qcount = sc->kbd.max_qcount = 0;
265 sc->aux.call_count = 0;
266 sc->aux.qcount = sc->aux.max_qcount = 0;
273 #if defined(__amd64__)
275 * On certain chipsets AT keyboard controller isn't present and is
276 * emulated by BIOS using SMI interrupt. On those chipsets reading
277 * from the status port may be thousand times slower than usually.
278 * Sometimes this emilation is not working properly resulting in
279 * commands timing our and since we assume that inb() operation
280 * takes very little time to complete we need to adjust number of
281 * retries to keep waiting time within a designed limits (100ms).
282 * Measure time it takes to make read_status() call and adjust
283 * number of retries accordingly.
285 flags = intr_disable();
292 read_delay = tscval[1] - tscval[0];
293 read_delay /= (tscval[2] - tscval[1]) / 1000;
294 sc->retry = 100000 / ((KBDD_DELAYTIME * 2) + read_delay);
302 /* open a keyboard controller */
304 atkbdc_open(int unit)
310 if ((atkbdc_softc[unit]->port0 != NULL)
311 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
312 return (KBDC)atkbdc_softc[unit];
317 * I/O access arbitration in `kbdio'
319 * The `kbdio' module uses a simplistic convention to arbitrate
320 * I/O access to the controller/keyboard/mouse. The convention requires
321 * close cooperation of the calling device driver.
323 * The device drivers which utilize the `kbdio' module are assumed to
324 * have the following set of routines.
325 * a. An interrupt handler (the bottom half of the driver).
326 * b. Timeout routines which may briefly poll the keyboard controller.
327 * c. Routines outside interrupt context (the top half of the driver).
328 * They should follow the rules below:
329 * 1. The interrupt handler may assume that it always has full access
330 * to the controller/keyboard/mouse.
331 * 2. The other routines must issue `spltty()' if they wish to
332 * prevent the interrupt handler from accessing
333 * the controller/keyboard/mouse.
334 * 3. The timeout routines and the top half routines of the device driver
335 * arbitrate I/O access by observing the lock flag in `kbdio'.
336 * The flag is manipulated via `kbdc_lock()'; when one wants to
337 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
338 * the call returns with TRUE. Otherwise the caller must back off.
339 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
340 * is finished. This mechanism does not prevent the interrupt
341 * handler from being invoked at any time and carrying out I/O.
342 * Therefore, `spltty()' must be strategically placed in the device
343 * driver code. Also note that the timeout routine may interrupt
344 * `kbdc_lock()' called by the top half of the driver, but this
345 * interruption is OK so long as the timeout routine observes
347 * 4. The interrupt and timeout routines should not extend I/O operation
348 * across more than one interrupt or timeout; they must complete any
349 * necessary I/O operation within one invocation of the routine.
350 * This means that if the timeout routine acquires the lock flag,
351 * it must reset the flag to FALSE before it returns.
354 /* set/reset polling lock */
356 kbdc_lock(KBDC p, int lock)
360 prevlock = kbdcp(p)->lock;
361 kbdcp(p)->lock = lock;
363 return (prevlock != lock);
366 /* check if any data is waiting to be processed */
368 kbdc_data_ready(KBDC p)
370 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
371 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
374 /* queuing functions */
377 addq(kqueue *q, int c)
379 if (nextq(q->tail) != q->head) {
381 q->tail = nextq(q->tail);
385 if (q->qcount > q->max_qcount)
386 q->max_qcount = q->qcount;
398 if (q->tail != q->head) {
400 q->head = nextq(q->head);
410 * device I/O routines
413 wait_while_controller_busy(struct atkbdc_softc *kbdc)
418 /* CPU will stay inside the loop for 100msec at most */
421 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
422 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
423 DELAY(KBDD_DELAYTIME);
424 addq(&kbdc->kbd, read_data(kbdc));
425 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
426 DELAY(KBDD_DELAYTIME);
427 addq(&kbdc->aux, read_data(kbdc));
429 DELAY(KBDC_DELAYTIME);
437 * wait for any data; whether it's from the controller,
438 * the keyboard, or the aux device.
441 wait_for_data(struct atkbdc_softc *kbdc)
446 /* CPU will stay inside the loop for 200msec at most */
447 retry = kbdc->retry * 2;
449 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
450 DELAY(KBDC_DELAYTIME);
454 DELAY(KBDD_DELAYTIME);
458 /* wait for data from the keyboard */
460 wait_for_kbd_data(struct atkbdc_softc *kbdc)
465 /* CPU will stay inside the loop for 200msec at most */
466 retry = kbdc->retry * 2;
468 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
469 != KBDS_KBD_BUFFER_FULL) {
470 if (f == KBDS_AUX_BUFFER_FULL) {
471 DELAY(KBDD_DELAYTIME);
472 addq(&kbdc->aux, read_data(kbdc));
474 DELAY(KBDC_DELAYTIME);
478 DELAY(KBDD_DELAYTIME);
483 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
484 * queue anything else.
487 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
493 /* CPU will stay inside the loop for 200msec at most */
494 retry = kbdc->retry * 2;
496 while (retry-- > 0) {
497 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
498 DELAY(KBDD_DELAYTIME);
500 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
501 if ((b == KBD_ACK) || (b == KBD_RESEND)
502 || (b == KBD_RESET_FAIL))
505 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
509 DELAY(KBDC_DELAYTIME);
514 /* wait for data from the aux device */
516 wait_for_aux_data(struct atkbdc_softc *kbdc)
521 /* CPU will stay inside the loop for 200msec at most */
522 retry = kbdc->retry * 2;
524 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
525 != KBDS_AUX_BUFFER_FULL) {
526 if (f == KBDS_KBD_BUFFER_FULL) {
527 DELAY(KBDD_DELAYTIME);
528 addq(&kbdc->kbd, read_data(kbdc));
530 DELAY(KBDC_DELAYTIME);
534 DELAY(KBDD_DELAYTIME);
539 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
540 * queue anything else.
543 wait_for_aux_ack(struct atkbdc_softc *kbdc)
549 /* CPU will stay inside the loop for 200msec at most */
550 retry = kbdc->retry * 2;
552 while (retry-- > 0) {
553 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
554 DELAY(KBDD_DELAYTIME);
556 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
557 if ((b == PSM_ACK) || (b == PSM_RESEND)
558 || (b == PSM_RESET_FAIL))
561 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
565 DELAY(KBDC_DELAYTIME);
570 /* write a one byte command to the controller */
572 write_controller_command(KBDC p, int c)
574 if (!wait_while_controller_busy(kbdcp(p)))
576 write_command(kbdcp(p), c);
580 /* write a one byte data to the controller */
582 write_controller_data(KBDC p, int c)
584 if (!wait_while_controller_busy(kbdcp(p)))
586 write_data(kbdcp(p), c);
590 /* write a one byte keyboard command */
592 write_kbd_command(KBDC p, int c)
594 if (!wait_while_controller_busy(kbdcp(p)))
596 write_data(kbdcp(p), c);
600 /* write a one byte auxiliary device command */
602 write_aux_command(KBDC p, int c)
604 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
606 return write_controller_data(p, c);
609 /* send a command to the keyboard and wait for ACK */
611 send_kbd_command(KBDC p, int c)
613 int retry = KBD_MAXRETRY;
616 while (retry-- > 0) {
617 if (!write_kbd_command(p, c))
619 res = wait_for_kbd_ack(kbdcp(p));
626 /* send a command to the auxiliary device and wait for ACK */
628 send_aux_command(KBDC p, int c)
630 int retry = KBD_MAXRETRY;
633 while (retry-- > 0) {
634 if (!write_aux_command(p, c))
638 * The aux device may have already sent one or two bytes of
639 * status data, when a command is received. It will immediately
640 * stop data transmission, thus, leaving an incomplete data
641 * packet in our buffer. We have to discard any unprocessed
642 * data in order to remove such packets. Well, we may remove
643 * unprocessed, but necessary data byte as well...
645 emptyq(&kbdcp(p)->aux);
646 res = wait_for_aux_ack(kbdcp(p));
653 /* send a command and a data to the keyboard, wait for ACKs */
655 send_kbd_command_and_data(KBDC p, int c, int d)
660 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
661 if (!write_kbd_command(p, c))
663 res = wait_for_kbd_ack(kbdcp(p));
666 else if (res != KBD_RESEND)
672 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
673 if (!write_kbd_command(p, d))
675 res = wait_for_kbd_ack(kbdcp(p));
676 if (res != KBD_RESEND)
682 /* send a command and a data to the auxiliary device, wait for ACKs */
684 send_aux_command_and_data(KBDC p, int c, int d)
689 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
690 if (!write_aux_command(p, c))
692 emptyq(&kbdcp(p)->aux);
693 res = wait_for_aux_ack(kbdcp(p));
696 else if (res != PSM_RESEND)
702 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
703 if (!write_aux_command(p, d))
705 res = wait_for_aux_ack(kbdcp(p));
706 if (res != PSM_RESEND)
713 * read one byte from any source; whether from the controller,
714 * the keyboard, or the aux device
717 read_controller_data(KBDC p)
719 if (availq(&kbdcp(p)->kbd))
720 return removeq(&kbdcp(p)->kbd);
721 if (availq(&kbdcp(p)->aux))
722 return removeq(&kbdcp(p)->aux);
723 if (!wait_for_data(kbdcp(p)))
724 return -1; /* timeout */
725 return read_data(kbdcp(p));
732 /* read one byte from the keyboard */
734 read_kbd_data(KBDC p)
739 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
740 "aux q: %d calls, max %d chars\n",
741 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
742 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
746 if (availq(&kbdcp(p)->kbd))
747 return removeq(&kbdcp(p)->kbd);
748 if (!wait_for_kbd_data(kbdcp(p)))
749 return -1; /* timeout */
750 return read_data(kbdcp(p));
753 /* read one byte from the keyboard, but return immediately if
757 read_kbd_data_no_wait(KBDC p)
764 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
765 "aux q: %d calls, max %d chars\n",
766 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
767 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
771 if (availq(&kbdcp(p)->kbd))
772 return removeq(&kbdcp(p)->kbd);
773 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
774 if (f == KBDS_AUX_BUFFER_FULL) {
775 DELAY(KBDD_DELAYTIME);
776 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
777 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
779 if (f == KBDS_KBD_BUFFER_FULL) {
780 DELAY(KBDD_DELAYTIME);
781 return read_data(kbdcp(p));
783 return -1; /* no data */
786 /* read one byte from the aux device */
788 read_aux_data(KBDC p)
790 if (availq(&kbdcp(p)->aux))
791 return removeq(&kbdcp(p)->aux);
792 if (!wait_for_aux_data(kbdcp(p)))
793 return -1; /* timeout */
794 return read_data(kbdcp(p));
797 /* read one byte from the aux device, but return immediately if
801 read_aux_data_no_wait(KBDC p)
805 if (availq(&kbdcp(p)->aux))
806 return removeq(&kbdcp(p)->aux);
807 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
808 if (f == KBDS_KBD_BUFFER_FULL) {
809 DELAY(KBDD_DELAYTIME);
810 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
811 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
813 if (f == KBDS_AUX_BUFFER_FULL) {
814 DELAY(KBDD_DELAYTIME);
815 return read_data(kbdcp(p));
817 return -1; /* no data */
820 /* discard data from the keyboard */
822 empty_kbd_buffer(KBDC p, int wait)
833 for (t = wait; t > 0; ) {
834 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
835 DELAY(KBDD_DELAYTIME);
836 b = read_data(kbdcp(p));
837 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
838 addq(&kbdcp(p)->aux, b);
852 if ((c1 > 0) || (c2 > 0))
853 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
856 emptyq(&kbdcp(p)->kbd);
859 /* discard data from the aux device */
861 empty_aux_buffer(KBDC p, int wait)
872 for (t = wait; t > 0; ) {
873 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
874 DELAY(KBDD_DELAYTIME);
875 b = read_data(kbdcp(p));
876 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
877 addq(&kbdcp(p)->kbd, b);
891 if ((c1 > 0) || (c2 > 0))
892 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
895 emptyq(&kbdcp(p)->aux);
898 /* discard any data from the keyboard or the aux device */
900 empty_both_buffers(KBDC p, int wait)
911 for (t = wait; t > 0; ) {
912 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
913 DELAY(KBDD_DELAYTIME);
914 (void)read_data(kbdcp(p));
916 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
927 * Some systems (Intel/IBM blades) do not have keyboard devices and
928 * will thus hang in this procedure. Time out after delta seconds to
929 * avoid this hang -- the keyboard attach will fail later on.
931 waited += (delta * 1000);
932 if (waited == (delta * 1000000))
938 if ((c1 > 0) || (c2 > 0))
939 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
942 emptyq(&kbdcp(p)->kbd);
943 emptyq(&kbdcp(p)->aux);
946 /* keyboard and mouse device control */
948 /* NOTE: enable the keyboard port but disable the keyboard
949 * interrupt before calling "reset_kbd()".
954 int retry = KBD_MAXRETRY;
955 int again = KBD_MAXWAIT;
956 int c = KBD_RESEND; /* keep the compiler happy */
958 while (retry-- > 0) {
959 empty_both_buffers(p, 10);
960 if (!write_kbd_command(p, KBDC_RESET_KBD))
962 emptyq(&kbdcp(p)->kbd);
963 c = read_controller_data(p);
964 if (verbose || bootverbose)
965 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
966 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
972 while (again-- > 0) {
973 /* wait awhile, well, in fact we must wait quite loooooooooooong */
974 DELAY(KBD_RESETDELAY*1000);
975 c = read_controller_data(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_KBD status:%04x\n", c);
981 if (c != KBD_RESET_DONE)
986 /* NOTE: enable the aux port but disable the aux interrupt
987 * before calling `reset_aux_dev()'.
990 reset_aux_dev(KBDC p)
992 int retry = KBD_MAXRETRY;
993 int again = KBD_MAXWAIT;
994 int c = PSM_RESEND; /* keep the compiler happy */
996 while (retry-- > 0) {
997 empty_both_buffers(p, 10);
998 if (!write_aux_command(p, PSMC_RESET_DEV))
1000 emptyq(&kbdcp(p)->aux);
1001 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
1002 for (again = KBD_MAXWAIT; again > 0; --again) {
1003 DELAY(KBD_RESETDELAY*1000);
1004 c = read_aux_data_no_wait(p);
1008 if (verbose || bootverbose)
1009 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
1010 if (c == PSM_ACK) /* aux dev is about to reset... */
1016 for (again = KBD_MAXWAIT; again > 0; --again) {
1017 /* wait awhile, well, quite looooooooooooong */
1018 DELAY(KBD_RESETDELAY*1000);
1019 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
1020 if (c != -1) /* wait again if the controller is not ready */
1023 if (verbose || bootverbose)
1024 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
1025 if (c != PSM_RESET_DONE) /* reset status */
1028 c = read_aux_data(p); /* device ID */
1029 if (verbose || bootverbose)
1030 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
1031 /* NOTE: we could check the device ID now, but leave it later... */
1035 /* controller diagnostics and setup */
1038 test_controller(KBDC p)
1040 int retry = KBD_MAXRETRY;
1041 int again = KBD_MAXWAIT;
1042 int c = KBD_DIAG_FAIL;
1044 while (retry-- > 0) {
1045 empty_both_buffers(p, 10);
1046 if (write_controller_command(p, KBDC_DIAGNOSE))
1052 emptyq(&kbdcp(p)->kbd);
1053 while (again-- > 0) {
1055 DELAY(KBD_RESETDELAY*1000);
1056 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
1057 if (c != -1) /* wait again if the controller is not ready */
1060 if (verbose || bootverbose)
1061 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
1062 return (c == KBD_DIAG_DONE);
1066 test_kbd_port(KBDC p)
1068 int retry = KBD_MAXRETRY;
1069 int again = KBD_MAXWAIT;
1072 while (retry-- > 0) {
1073 empty_both_buffers(p, 10);
1074 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1080 emptyq(&kbdcp(p)->kbd);
1081 while (again-- > 0) {
1082 c = read_controller_data(p);
1083 if (c != -1) /* try again if the controller is not ready */
1086 if (verbose || bootverbose)
1087 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1092 test_aux_port(KBDC p)
1094 int retry = KBD_MAXRETRY;
1095 int again = KBD_MAXWAIT;
1098 while (retry-- > 0) {
1099 empty_both_buffers(p, 10);
1100 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1106 emptyq(&kbdcp(p)->kbd);
1107 while (again-- > 0) {
1108 c = read_controller_data(p);
1109 if (c != -1) /* try again if the controller is not ready */
1112 if (verbose || bootverbose)
1113 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1118 kbdc_get_device_mask(KBDC p)
1120 return kbdcp(p)->command_mask;
1124 kbdc_set_device_mask(KBDC p, int mask)
1126 kbdcp(p)->command_mask =
1127 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1131 get_controller_command_byte(KBDC p)
1133 if (kbdcp(p)->command_byte != -1)
1134 return kbdcp(p)->command_byte;
1135 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1137 emptyq(&kbdcp(p)->kbd);
1138 kbdcp(p)->command_byte = read_controller_data(p);
1139 return kbdcp(p)->command_byte;
1143 set_controller_command_byte(KBDC p, int mask, int command)
1145 if (get_controller_command_byte(p) == -1)
1148 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1149 if (command & KBD_DISABLE_KBD_PORT) {
1150 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1153 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1155 if (!write_controller_data(p, command))
1157 kbdcp(p)->command_byte = command;
1160 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",