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);
117 struct atkbdc_quirks {
118 const char* bios_vendor;
124 static struct atkbdc_quirks quirks[] = {
125 {"coreboot", NULL, NULL,
126 KBDC_QUIRK_KEEP_ACTIVATED | KBDC_QUIRK_IGNORE_PROBE_RESULT |
127 KBDC_QUIRK_RESET_AFTER_PROBE | KBDC_QUIRK_SETLEDS_ON_INIT},
129 {NULL, NULL, NULL, 0}
132 #define QUIRK_STR_MATCH(s1, s2) (s1 == NULL || \
133 (s2 != NULL && !strcmp(s1, s2)))
136 atkbdc_getquirks(void)
139 char* bios_vendor = kern_getenv("smbios.bios.vendor");
140 char* maker = kern_getenv("smbios.system.maker");
141 char* product = kern_getenv("smbios.system.product");
143 for (i=0; quirks[i].quirk != 0; ++i)
144 if (QUIRK_STR_MATCH(quirks[i].bios_vendor, bios_vendor) &&
145 QUIRK_STR_MATCH(quirks[i].maker, maker) &&
146 QUIRK_STR_MATCH(quirks[i].product, product))
147 return (quirks[i].quirk);
153 *atkbdc_get_softc(int unit)
157 if (unit >= nitems(atkbdc_softc))
159 sc = atkbdc_softc[unit];
161 sc = atkbdc_softc[unit]
162 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO);
170 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
172 if (rman_get_start(port0) <= 0)
174 if (rman_get_start(port1) <= 0)
180 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
181 struct resource *port1)
183 return atkbdc_setup(sc, rman_get_bustag(port0),
184 rman_get_bushandle(port0),
185 rman_get_bushandle(port1));
188 /* the backdoor to the keyboard controller! XXX */
190 atkbdc_configure(void)
193 bus_space_handle_t h0;
194 bus_space_handle_t h1;
195 #if defined(__i386__) || defined(__amd64__)
201 phandle_t chosen, node;
211 /* XXX: tag should be passed from the caller */
212 #if defined(__amd64__) || defined(__i386__)
213 tag = X86_BUS_SPACE_IO;
214 #elif defined(__sparc64__)
215 tag = &atkbdc_bst_store[0];
221 if ((chosen = OF_finddevice("/chosen")) == -1)
223 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1)
225 if ((node = OF_instance_to_package(stdin)) == -1)
227 if (OF_getprop(node, "name", name, sizeof(name)) == -1)
229 name[sizeof(name) - 1] = '\0';
230 if (strcmp(name, "kb_ps2") != 0)
233 * The stdin handle points to an instance of a PS/2 keyboard
234 * package but we want the 8042 controller, which is the parent
235 * of that keyboard node.
237 if ((node = OF_parent(node)) == 0)
239 if (OF_decode_addr(node, 0, &space, &port0) != 0)
241 h0 = sparc64_fake_bustag(space, port0, tag);
242 bus_space_subregion(tag, h0, KBD_DATA_PORT, 1, &h0);
243 if (OF_decode_addr(node, 1, &space, &port1) != 0)
245 h1 = sparc64_fake_bustag(space, port1, tag);
246 bus_space_subregion(tag, h1, KBD_STATUS_PORT, 1, &h1);
249 resource_int_value("atkbdc", 0, "port", &port0);
250 port1 = IO_KBD + KBD_STATUS_PORT;
252 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
253 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
255 h0 = (bus_space_handle_t)port0;
256 h1 = (bus_space_handle_t)port1;
260 #if defined(__i386__) || defined(__amd64__)
262 * Check if we really have AT keyboard controller. Poll status
263 * register until we get "all clear" indication. If no such
264 * indication comes, it probably means that there is no AT
265 * keyboard controller present. Give up in such case. Check relies
266 * on the fact that reading from non-existing in/out port returns
267 * 0xff on i386. May or may not be true on other platforms.
269 flags = intr_disable();
270 for (i = 0; i != 65535; i++) {
271 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
279 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
283 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
284 bus_space_handle_t h1)
286 #if defined(__amd64__)
287 u_int64_t tscval[3], read_delay;
291 if (sc->ioh0 == 0) { /* XXX */
292 sc->command_byte = -1;
293 sc->command_mask = 0;
295 sc->kbd.head = sc->kbd.tail = 0;
296 sc->aux.head = sc->aux.tail = 0;
298 sc->kbd.call_count = 0;
299 sc->kbd.qcount = sc->kbd.max_qcount = 0;
300 sc->aux.call_count = 0;
301 sc->aux.qcount = sc->aux.max_qcount = 0;
308 #if defined(__amd64__)
310 * On certain chipsets AT keyboard controller isn't present and is
311 * emulated by BIOS using SMI interrupt. On those chipsets reading
312 * from the status port may be thousand times slower than usually.
313 * Sometimes this emilation is not working properly resulting in
314 * commands timing our and since we assume that inb() operation
315 * takes very little time to complete we need to adjust number of
316 * retries to keep waiting time within a designed limits (100ms).
317 * Measure time it takes to make read_status() call and adjust
318 * number of retries accordingly.
320 flags = intr_disable();
327 read_delay = tscval[1] - tscval[0];
328 read_delay /= (tscval[2] - tscval[1]) / 1000;
329 sc->retry = 100000 / ((KBDD_DELAYTIME * 2) + read_delay);
333 sc->quirks = atkbdc_getquirks();
338 /* open a keyboard controller */
340 atkbdc_open(int unit)
346 if ((atkbdc_softc[unit]->port0 != NULL)
347 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
348 return (KBDC)atkbdc_softc[unit];
353 * I/O access arbitration in `kbdio'
355 * The `kbdio' module uses a simplistic convention to arbitrate
356 * I/O access to the controller/keyboard/mouse. The convention requires
357 * close cooperation of the calling device driver.
359 * The device drivers which utilize the `kbdio' module are assumed to
360 * have the following set of routines.
361 * a. An interrupt handler (the bottom half of the driver).
362 * b. Timeout routines which may briefly poll the keyboard controller.
363 * c. Routines outside interrupt context (the top half of the driver).
364 * They should follow the rules below:
365 * 1. The interrupt handler may assume that it always has full access
366 * to the controller/keyboard/mouse.
367 * 2. The other routines must issue `spltty()' if they wish to
368 * prevent the interrupt handler from accessing
369 * the controller/keyboard/mouse.
370 * 3. The timeout routines and the top half routines of the device driver
371 * arbitrate I/O access by observing the lock flag in `kbdio'.
372 * The flag is manipulated via `kbdc_lock()'; when one wants to
373 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
374 * the call returns with TRUE. Otherwise the caller must back off.
375 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
376 * is finished. This mechanism does not prevent the interrupt
377 * handler from being invoked at any time and carrying out I/O.
378 * Therefore, `spltty()' must be strategically placed in the device
379 * driver code. Also note that the timeout routine may interrupt
380 * `kbdc_lock()' called by the top half of the driver, but this
381 * interruption is OK so long as the timeout routine observes
383 * 4. The interrupt and timeout routines should not extend I/O operation
384 * across more than one interrupt or timeout; they must complete any
385 * necessary I/O operation within one invocation of the routine.
386 * This means that if the timeout routine acquires the lock flag,
387 * it must reset the flag to FALSE before it returns.
390 /* set/reset polling lock */
392 kbdc_lock(KBDC p, int lock)
396 prevlock = kbdcp(p)->lock;
397 kbdcp(p)->lock = lock;
399 return (prevlock != lock);
402 /* check if any data is waiting to be processed */
404 kbdc_data_ready(KBDC p)
406 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
407 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
410 /* queuing functions */
413 addq(kqueue *q, int c)
415 if (nextq(q->tail) != q->head) {
417 q->tail = nextq(q->tail);
421 if (q->qcount > q->max_qcount)
422 q->max_qcount = q->qcount;
434 if (q->tail != q->head) {
436 q->head = nextq(q->head);
446 * device I/O routines
449 wait_while_controller_busy(struct atkbdc_softc *kbdc)
454 /* CPU will stay inside the loop for 100msec at most */
457 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
458 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
459 DELAY(KBDD_DELAYTIME);
460 addq(&kbdc->kbd, read_data(kbdc));
461 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
462 DELAY(KBDD_DELAYTIME);
463 addq(&kbdc->aux, read_data(kbdc));
465 DELAY(KBDC_DELAYTIME);
473 * wait for any data; whether it's from the controller,
474 * the keyboard, or the aux device.
477 wait_for_data(struct atkbdc_softc *kbdc)
482 /* CPU will stay inside the loop for 200msec at most */
483 retry = kbdc->retry * 2;
485 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
486 DELAY(KBDC_DELAYTIME);
490 DELAY(KBDD_DELAYTIME);
494 /* wait for data from the keyboard */
496 wait_for_kbd_data(struct atkbdc_softc *kbdc)
501 /* CPU will stay inside the loop for 200msec at most */
502 retry = kbdc->retry * 2;
504 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
505 != KBDS_KBD_BUFFER_FULL) {
506 if (f == KBDS_AUX_BUFFER_FULL) {
507 DELAY(KBDD_DELAYTIME);
508 addq(&kbdc->aux, read_data(kbdc));
510 DELAY(KBDC_DELAYTIME);
514 DELAY(KBDD_DELAYTIME);
519 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
520 * queue anything else.
523 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
529 /* CPU will stay inside the loop for 200msec at most */
530 retry = kbdc->retry * 2;
532 while (retry-- > 0) {
533 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
534 DELAY(KBDD_DELAYTIME);
536 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
537 if ((b == KBD_ACK) || (b == KBD_RESEND)
538 || (b == KBD_RESET_FAIL))
541 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
545 DELAY(KBDC_DELAYTIME);
550 /* wait for data from the aux device */
552 wait_for_aux_data(struct atkbdc_softc *kbdc)
557 /* CPU will stay inside the loop for 200msec at most */
558 retry = kbdc->retry * 2;
560 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
561 != KBDS_AUX_BUFFER_FULL) {
562 if (f == KBDS_KBD_BUFFER_FULL) {
563 DELAY(KBDD_DELAYTIME);
564 addq(&kbdc->kbd, read_data(kbdc));
566 DELAY(KBDC_DELAYTIME);
570 DELAY(KBDD_DELAYTIME);
575 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
576 * queue anything else.
579 wait_for_aux_ack(struct atkbdc_softc *kbdc)
585 /* CPU will stay inside the loop for 200msec at most */
586 retry = kbdc->retry * 2;
588 while (retry-- > 0) {
589 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
590 DELAY(KBDD_DELAYTIME);
592 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
593 if ((b == PSM_ACK) || (b == PSM_RESEND)
594 || (b == PSM_RESET_FAIL))
597 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
601 DELAY(KBDC_DELAYTIME);
606 /* write a one byte command to the controller */
608 write_controller_command(KBDC p, int c)
610 if (!wait_while_controller_busy(kbdcp(p)))
612 write_command(kbdcp(p), c);
616 /* write a one byte data to the controller */
618 write_controller_data(KBDC p, int c)
620 if (!wait_while_controller_busy(kbdcp(p)))
622 write_data(kbdcp(p), c);
626 /* write a one byte keyboard command */
628 write_kbd_command(KBDC p, int c)
630 if (!wait_while_controller_busy(kbdcp(p)))
632 write_data(kbdcp(p), c);
636 /* write a one byte auxiliary device command */
638 write_aux_command(KBDC p, int c)
640 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
642 return write_controller_data(p, c);
645 /* send a command to the keyboard and wait for ACK */
647 send_kbd_command(KBDC p, int c)
649 int retry = KBD_MAXRETRY;
652 while (retry-- > 0) {
653 if (!write_kbd_command(p, c))
655 res = wait_for_kbd_ack(kbdcp(p));
662 /* send a command to the auxiliary device and wait for ACK */
664 send_aux_command(KBDC p, int c)
666 int retry = KBD_MAXRETRY;
669 while (retry-- > 0) {
670 if (!write_aux_command(p, c))
674 * The aux device may have already sent one or two bytes of
675 * status data, when a command is received. It will immediately
676 * stop data transmission, thus, leaving an incomplete data
677 * packet in our buffer. We have to discard any unprocessed
678 * data in order to remove such packets. Well, we may remove
679 * unprocessed, but necessary data byte as well...
681 emptyq(&kbdcp(p)->aux);
682 res = wait_for_aux_ack(kbdcp(p));
689 /* send a command and a data to the keyboard, wait for ACKs */
691 send_kbd_command_and_data(KBDC p, int c, int d)
696 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
697 if (!write_kbd_command(p, c))
699 res = wait_for_kbd_ack(kbdcp(p));
702 else if (res != KBD_RESEND)
708 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
709 if (!write_kbd_command(p, d))
711 res = wait_for_kbd_ack(kbdcp(p));
712 if (res != KBD_RESEND)
718 /* send a command and a data to the auxiliary device, wait for ACKs */
720 send_aux_command_and_data(KBDC p, int c, int d)
725 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
726 if (!write_aux_command(p, c))
728 emptyq(&kbdcp(p)->aux);
729 res = wait_for_aux_ack(kbdcp(p));
732 else if (res != PSM_RESEND)
738 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
739 if (!write_aux_command(p, d))
741 res = wait_for_aux_ack(kbdcp(p));
742 if (res != PSM_RESEND)
749 * read one byte from any source; whether from the controller,
750 * the keyboard, or the aux device
753 read_controller_data(KBDC p)
755 if (availq(&kbdcp(p)->kbd))
756 return removeq(&kbdcp(p)->kbd);
757 if (availq(&kbdcp(p)->aux))
758 return removeq(&kbdcp(p)->aux);
759 if (!wait_for_data(kbdcp(p)))
760 return -1; /* timeout */
761 return read_data(kbdcp(p));
768 /* read one byte from the keyboard */
770 read_kbd_data(KBDC p)
775 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
776 "aux q: %d calls, max %d chars\n",
777 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
778 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
782 if (availq(&kbdcp(p)->kbd))
783 return removeq(&kbdcp(p)->kbd);
784 if (!wait_for_kbd_data(kbdcp(p)))
785 return -1; /* timeout */
786 return read_data(kbdcp(p));
789 /* read one byte from the keyboard, but return immediately if
793 read_kbd_data_no_wait(KBDC p)
800 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
801 "aux q: %d calls, max %d chars\n",
802 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
803 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
807 if (availq(&kbdcp(p)->kbd))
808 return removeq(&kbdcp(p)->kbd);
809 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
810 if (f == KBDS_AUX_BUFFER_FULL) {
811 DELAY(KBDD_DELAYTIME);
812 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
813 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
815 if (f == KBDS_KBD_BUFFER_FULL) {
816 DELAY(KBDD_DELAYTIME);
817 return read_data(kbdcp(p));
819 return -1; /* no data */
822 /* read one byte from the aux device */
824 read_aux_data(KBDC p)
826 if (availq(&kbdcp(p)->aux))
827 return removeq(&kbdcp(p)->aux);
828 if (!wait_for_aux_data(kbdcp(p)))
829 return -1; /* timeout */
830 return read_data(kbdcp(p));
833 /* read one byte from the aux device, but return immediately if
837 read_aux_data_no_wait(KBDC p)
841 if (availq(&kbdcp(p)->aux))
842 return removeq(&kbdcp(p)->aux);
843 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
844 if (f == KBDS_KBD_BUFFER_FULL) {
845 DELAY(KBDD_DELAYTIME);
846 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
847 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
849 if (f == KBDS_AUX_BUFFER_FULL) {
850 DELAY(KBDD_DELAYTIME);
851 return read_data(kbdcp(p));
853 return -1; /* no data */
856 /* discard data from the keyboard */
858 empty_kbd_buffer(KBDC p, int wait)
869 for (t = wait; t > 0; ) {
870 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
871 DELAY(KBDD_DELAYTIME);
872 b = read_data(kbdcp(p));
873 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
874 addq(&kbdcp(p)->aux, b);
888 if ((c1 > 0) || (c2 > 0))
889 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
892 emptyq(&kbdcp(p)->kbd);
895 /* discard data from the aux device */
897 empty_aux_buffer(KBDC p, int wait)
908 for (t = wait; t > 0; ) {
909 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
910 DELAY(KBDD_DELAYTIME);
911 b = read_data(kbdcp(p));
912 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
913 addq(&kbdcp(p)->kbd, b);
927 if ((c1 > 0) || (c2 > 0))
928 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
931 emptyq(&kbdcp(p)->aux);
934 /* discard any data from the keyboard or the aux device */
936 empty_both_buffers(KBDC p, int wait)
947 for (t = wait; t > 0; ) {
948 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
949 DELAY(KBDD_DELAYTIME);
950 (void)read_data(kbdcp(p));
952 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
963 * Some systems (Intel/IBM blades) do not have keyboard devices and
964 * will thus hang in this procedure. Time out after delta seconds to
965 * avoid this hang -- the keyboard attach will fail later on.
967 waited += (delta * 1000);
968 if (waited == (delta * 1000000))
974 if ((c1 > 0) || (c2 > 0))
975 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
978 emptyq(&kbdcp(p)->kbd);
979 emptyq(&kbdcp(p)->aux);
982 /* keyboard and mouse device control */
984 /* NOTE: enable the keyboard port but disable the keyboard
985 * interrupt before calling "reset_kbd()".
990 int retry = KBD_MAXRETRY;
991 int again = KBD_MAXWAIT;
992 int c = KBD_RESEND; /* keep the compiler happy */
994 while (retry-- > 0) {
995 empty_both_buffers(p, 10);
996 if (!write_kbd_command(p, KBDC_RESET_KBD))
998 emptyq(&kbdcp(p)->kbd);
999 c = read_controller_data(p);
1000 if (verbose || bootverbose)
1001 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
1002 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
1008 while (again-- > 0) {
1009 /* wait awhile, well, in fact we must wait quite loooooooooooong */
1010 DELAY(KBD_RESETDELAY*1000);
1011 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
1012 if (c != -1) /* wait again if the controller is not ready */
1015 if (verbose || bootverbose)
1016 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
1017 if (c != KBD_RESET_DONE)
1022 /* NOTE: enable the aux port but disable the aux interrupt
1023 * before calling `reset_aux_dev()'.
1026 reset_aux_dev(KBDC p)
1028 int retry = KBD_MAXRETRY;
1029 int again = KBD_MAXWAIT;
1030 int c = PSM_RESEND; /* keep the compiler happy */
1032 while (retry-- > 0) {
1033 empty_both_buffers(p, 10);
1034 if (!write_aux_command(p, PSMC_RESET_DEV))
1036 emptyq(&kbdcp(p)->aux);
1037 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
1038 for (again = KBD_MAXWAIT; again > 0; --again) {
1039 DELAY(KBD_RESETDELAY*1000);
1040 c = read_aux_data_no_wait(p);
1044 if (verbose || bootverbose)
1045 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
1046 if (c == PSM_ACK) /* aux dev is about to reset... */
1052 for (again = KBD_MAXWAIT; again > 0; --again) {
1053 /* wait awhile, well, quite looooooooooooong */
1054 DELAY(KBD_RESETDELAY*1000);
1055 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
1056 if (c != -1) /* wait again if the controller is not ready */
1059 if (verbose || bootverbose)
1060 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
1061 if (c != PSM_RESET_DONE) /* reset status */
1064 c = read_aux_data(p); /* device ID */
1065 if (verbose || bootverbose)
1066 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
1067 /* NOTE: we could check the device ID now, but leave it later... */
1071 /* controller diagnostics and setup */
1074 test_controller(KBDC p)
1076 int retry = KBD_MAXRETRY;
1077 int again = KBD_MAXWAIT;
1078 int c = KBD_DIAG_FAIL;
1080 while (retry-- > 0) {
1081 empty_both_buffers(p, 10);
1082 if (write_controller_command(p, KBDC_DIAGNOSE))
1088 emptyq(&kbdcp(p)->kbd);
1089 while (again-- > 0) {
1091 DELAY(KBD_RESETDELAY*1000);
1092 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
1093 if (c != -1) /* wait again if the controller is not ready */
1096 if (verbose || bootverbose)
1097 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
1098 return (c == KBD_DIAG_DONE);
1102 test_kbd_port(KBDC p)
1104 int retry = KBD_MAXRETRY;
1105 int again = KBD_MAXWAIT;
1108 while (retry-- > 0) {
1109 empty_both_buffers(p, 10);
1110 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1116 emptyq(&kbdcp(p)->kbd);
1117 while (again-- > 0) {
1118 c = read_controller_data(p);
1119 if (c != -1) /* try again if the controller is not ready */
1122 if (verbose || bootverbose)
1123 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1128 test_aux_port(KBDC p)
1130 int retry = KBD_MAXRETRY;
1131 int again = KBD_MAXWAIT;
1134 while (retry-- > 0) {
1135 empty_both_buffers(p, 10);
1136 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1142 emptyq(&kbdcp(p)->kbd);
1143 while (again-- > 0) {
1144 c = read_controller_data(p);
1145 if (c != -1) /* try again if the controller is not ready */
1148 if (verbose || bootverbose)
1149 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1154 kbdc_get_device_mask(KBDC p)
1156 return kbdcp(p)->command_mask;
1160 kbdc_set_device_mask(KBDC p, int mask)
1162 kbdcp(p)->command_mask =
1163 mask & (((kbdcp(p)->quirks & KBDC_QUIRK_KEEP_ACTIVATED)
1164 ? 0 : KBD_KBD_CONTROL_BITS) | KBD_AUX_CONTROL_BITS);
1168 get_controller_command_byte(KBDC p)
1170 if (kbdcp(p)->command_byte != -1)
1171 return kbdcp(p)->command_byte;
1172 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1174 emptyq(&kbdcp(p)->kbd);
1175 kbdcp(p)->command_byte = read_controller_data(p);
1176 return kbdcp(p)->command_byte;
1180 set_controller_command_byte(KBDC p, int mask, int command)
1182 if (get_controller_command_byte(p) == -1)
1185 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1186 if (command & KBD_DISABLE_KBD_PORT) {
1187 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1190 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1192 if (!write_controller_data(p, command))
1194 kbdcp(p)->command_byte = command;
1197 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",