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", "Acer", "Peppy",
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;
297 sc->aux_mux_enabled = FALSE;
299 sc->kbd.call_count = 0;
300 sc->kbd.qcount = sc->kbd.max_qcount = 0;
301 sc->aux.call_count = 0;
302 sc->aux.qcount = sc->aux.max_qcount = 0;
309 #if defined(__amd64__)
311 * On certain chipsets AT keyboard controller isn't present and is
312 * emulated by BIOS using SMI interrupt. On those chipsets reading
313 * from the status port may be thousand times slower than usually.
314 * Sometimes this emilation is not working properly resulting in
315 * commands timing our and since we assume that inb() operation
316 * takes very little time to complete we need to adjust number of
317 * retries to keep waiting time within a designed limits (100ms).
318 * Measure time it takes to make read_status() call and adjust
319 * number of retries accordingly.
321 flags = intr_disable();
328 read_delay = tscval[1] - tscval[0];
329 read_delay /= (tscval[2] - tscval[1]) / 1000;
330 sc->retry = 100000 / ((KBDD_DELAYTIME * 2) + read_delay);
334 sc->quirks = atkbdc_getquirks();
339 /* open a keyboard controller */
341 atkbdc_open(int unit)
347 if ((atkbdc_softc[unit]->port0 != NULL)
348 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
349 return (KBDC)atkbdc_softc[unit];
354 * I/O access arbitration in `kbdio'
356 * The `kbdio' module uses a simplistic convention to arbitrate
357 * I/O access to the controller/keyboard/mouse. The convention requires
358 * close cooperation of the calling device driver.
360 * The device drivers which utilize the `kbdio' module are assumed to
361 * have the following set of routines.
362 * a. An interrupt handler (the bottom half of the driver).
363 * b. Timeout routines which may briefly poll the keyboard controller.
364 * c. Routines outside interrupt context (the top half of the driver).
365 * They should follow the rules below:
366 * 1. The interrupt handler may assume that it always has full access
367 * to the controller/keyboard/mouse.
368 * 2. The other routines must issue `spltty()' if they wish to
369 * prevent the interrupt handler from accessing
370 * the controller/keyboard/mouse.
371 * 3. The timeout routines and the top half routines of the device driver
372 * arbitrate I/O access by observing the lock flag in `kbdio'.
373 * The flag is manipulated via `kbdc_lock()'; when one wants to
374 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
375 * the call returns with TRUE. Otherwise the caller must back off.
376 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
377 * is finished. This mechanism does not prevent the interrupt
378 * handler from being invoked at any time and carrying out I/O.
379 * Therefore, `spltty()' must be strategically placed in the device
380 * driver code. Also note that the timeout routine may interrupt
381 * `kbdc_lock()' called by the top half of the driver, but this
382 * interruption is OK so long as the timeout routine observes
384 * 4. The interrupt and timeout routines should not extend I/O operation
385 * across more than one interrupt or timeout; they must complete any
386 * necessary I/O operation within one invocation of the routine.
387 * This means that if the timeout routine acquires the lock flag,
388 * it must reset the flag to FALSE before it returns.
391 /* set/reset polling lock */
393 kbdc_lock(KBDC p, int lock)
397 prevlock = kbdcp(p)->lock;
398 kbdcp(p)->lock = lock;
400 return (prevlock != lock);
403 /* check if any data is waiting to be processed */
405 kbdc_data_ready(KBDC p)
407 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
408 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
411 /* queuing functions */
414 addq(kqueue *q, int c)
416 if (nextq(q->tail) != q->head) {
418 q->tail = nextq(q->tail);
422 if (q->qcount > q->max_qcount)
423 q->max_qcount = q->qcount;
435 if (q->tail != q->head) {
437 q->head = nextq(q->head);
447 * device I/O routines
450 wait_while_controller_busy(struct atkbdc_softc *kbdc)
455 /* CPU will stay inside the loop for 100msec at most */
458 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
459 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
460 DELAY(KBDD_DELAYTIME);
461 addq(&kbdc->kbd, read_data(kbdc));
462 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
463 DELAY(KBDD_DELAYTIME);
464 addq(&kbdc->aux, read_data(kbdc));
466 DELAY(KBDC_DELAYTIME);
474 * wait for any data; whether it's from the controller,
475 * the keyboard, or the aux device.
478 wait_for_data(struct atkbdc_softc *kbdc)
483 /* CPU will stay inside the loop for 200msec at most */
484 retry = kbdc->retry * 2;
486 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
487 DELAY(KBDC_DELAYTIME);
491 DELAY(KBDD_DELAYTIME);
495 /* wait for data from the keyboard */
497 wait_for_kbd_data(struct atkbdc_softc *kbdc)
502 /* CPU will stay inside the loop for 200msec at most */
503 retry = kbdc->retry * 2;
505 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
506 != KBDS_KBD_BUFFER_FULL) {
507 if (f == KBDS_AUX_BUFFER_FULL) {
508 DELAY(KBDD_DELAYTIME);
509 addq(&kbdc->aux, read_data(kbdc));
511 DELAY(KBDC_DELAYTIME);
515 DELAY(KBDD_DELAYTIME);
520 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
521 * queue anything else.
524 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
530 /* CPU will stay inside the loop for 200msec at most */
531 retry = kbdc->retry * 2;
533 while (retry-- > 0) {
534 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
535 DELAY(KBDD_DELAYTIME);
537 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
538 if ((b == KBD_ACK) || (b == KBD_RESEND)
539 || (b == KBD_RESET_FAIL))
542 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
546 DELAY(KBDC_DELAYTIME);
551 /* wait for data from the aux device */
553 wait_for_aux_data(struct atkbdc_softc *kbdc)
558 /* CPU will stay inside the loop for 200msec at most */
559 retry = kbdc->retry * 2;
561 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
562 != KBDS_AUX_BUFFER_FULL) {
563 if (f == KBDS_KBD_BUFFER_FULL) {
564 DELAY(KBDD_DELAYTIME);
565 addq(&kbdc->kbd, read_data(kbdc));
567 DELAY(KBDC_DELAYTIME);
571 DELAY(KBDD_DELAYTIME);
576 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
577 * queue anything else.
580 wait_for_aux_ack(struct atkbdc_softc *kbdc)
586 /* CPU will stay inside the loop for 200msec at most */
587 retry = kbdc->retry * 2;
589 while (retry-- > 0) {
590 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
591 DELAY(KBDD_DELAYTIME);
593 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
594 if ((b == PSM_ACK) || (b == PSM_RESEND)
595 || (b == PSM_RESET_FAIL))
598 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
602 DELAY(KBDC_DELAYTIME);
607 /* write a one byte command to the controller */
609 write_controller_command(KBDC p, int c)
611 if (!wait_while_controller_busy(kbdcp(p)))
613 write_command(kbdcp(p), c);
617 /* write a one byte data to the controller */
619 write_controller_data(KBDC p, int c)
621 if (!wait_while_controller_busy(kbdcp(p)))
623 write_data(kbdcp(p), c);
627 /* write a one byte keyboard command */
629 write_kbd_command(KBDC p, int c)
631 if (!wait_while_controller_busy(kbdcp(p)))
633 write_data(kbdcp(p), c);
637 /* write a one byte auxiliary device command */
639 write_aux_command(KBDC p, int c)
643 f = aux_mux_is_enabled(p) ?
644 KBDC_WRITE_TO_AUX_MUX + kbdcp(p)->aux_mux_port : KBDC_WRITE_TO_AUX;
646 if (!write_controller_command(p, f))
648 return write_controller_data(p, c);
651 /* send a command to the keyboard and wait for ACK */
653 send_kbd_command(KBDC p, int c)
655 int retry = KBD_MAXRETRY;
658 while (retry-- > 0) {
659 if (!write_kbd_command(p, c))
661 res = wait_for_kbd_ack(kbdcp(p));
668 /* send a command to the auxiliary device and wait for ACK */
670 send_aux_command(KBDC p, int c)
672 int retry = KBD_MAXRETRY;
675 while (retry-- > 0) {
676 if (!write_aux_command(p, c))
680 * The aux device may have already sent one or two bytes of
681 * status data, when a command is received. It will immediately
682 * stop data transmission, thus, leaving an incomplete data
683 * packet in our buffer. We have to discard any unprocessed
684 * data in order to remove such packets. Well, we may remove
685 * unprocessed, but necessary data byte as well...
687 emptyq(&kbdcp(p)->aux);
688 res = wait_for_aux_ack(kbdcp(p));
695 /* send a command and a data to the keyboard, wait for ACKs */
697 send_kbd_command_and_data(KBDC p, int c, int d)
702 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
703 if (!write_kbd_command(p, c))
705 res = wait_for_kbd_ack(kbdcp(p));
708 else if (res != KBD_RESEND)
714 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
715 if (!write_kbd_command(p, d))
717 res = wait_for_kbd_ack(kbdcp(p));
718 if (res != KBD_RESEND)
724 /* send a command and a data to the auxiliary device, wait for ACKs */
726 send_aux_command_and_data(KBDC p, int c, int d)
731 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
732 if (!write_aux_command(p, c))
734 emptyq(&kbdcp(p)->aux);
735 res = wait_for_aux_ack(kbdcp(p));
738 else if (res != PSM_RESEND)
744 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
745 if (!write_aux_command(p, d))
747 res = wait_for_aux_ack(kbdcp(p));
748 if (res != PSM_RESEND)
755 * read one byte from any source; whether from the controller,
756 * the keyboard, or the aux device
759 read_controller_data(KBDC p)
761 if (availq(&kbdcp(p)->kbd))
762 return removeq(&kbdcp(p)->kbd);
763 if (availq(&kbdcp(p)->aux))
764 return removeq(&kbdcp(p)->aux);
765 if (!wait_for_data(kbdcp(p)))
766 return -1; /* timeout */
767 return read_data(kbdcp(p));
774 /* read one byte from the keyboard */
776 read_kbd_data(KBDC p)
781 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
782 "aux q: %d calls, max %d chars\n",
783 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
784 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
788 if (availq(&kbdcp(p)->kbd))
789 return removeq(&kbdcp(p)->kbd);
790 if (!wait_for_kbd_data(kbdcp(p)))
791 return -1; /* timeout */
792 return read_data(kbdcp(p));
795 /* read one byte from the keyboard, but return immediately if
799 read_kbd_data_no_wait(KBDC p)
806 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
807 "aux q: %d calls, max %d chars\n",
808 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
809 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
813 if (availq(&kbdcp(p)->kbd))
814 return removeq(&kbdcp(p)->kbd);
815 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
816 if (f == KBDS_AUX_BUFFER_FULL) {
817 DELAY(KBDD_DELAYTIME);
818 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
819 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
821 if (f == KBDS_KBD_BUFFER_FULL) {
822 DELAY(KBDD_DELAYTIME);
823 return read_data(kbdcp(p));
825 return -1; /* no data */
828 /* read one byte from the aux device */
830 read_aux_data(KBDC p)
832 if (availq(&kbdcp(p)->aux))
833 return removeq(&kbdcp(p)->aux);
834 if (!wait_for_aux_data(kbdcp(p)))
835 return -1; /* timeout */
836 return read_data(kbdcp(p));
839 /* read one byte from the aux device, but return immediately if
843 read_aux_data_no_wait(KBDC p)
847 if (availq(&kbdcp(p)->aux))
848 return removeq(&kbdcp(p)->aux);
849 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
850 if (f == KBDS_KBD_BUFFER_FULL) {
851 DELAY(KBDD_DELAYTIME);
852 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
853 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
855 if (f == KBDS_AUX_BUFFER_FULL) {
856 DELAY(KBDD_DELAYTIME);
857 return read_data(kbdcp(p));
859 return -1; /* no data */
862 /* discard data from the keyboard */
864 empty_kbd_buffer(KBDC p, int wait)
875 for (t = wait; t > 0; ) {
876 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
877 DELAY(KBDD_DELAYTIME);
878 b = read_data(kbdcp(p));
879 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
880 addq(&kbdcp(p)->aux, b);
894 if ((c1 > 0) || (c2 > 0))
895 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
898 emptyq(&kbdcp(p)->kbd);
901 /* discard data from the aux device */
903 empty_aux_buffer(KBDC p, int wait)
914 for (t = wait; t > 0; ) {
915 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
916 DELAY(KBDD_DELAYTIME);
917 b = read_data(kbdcp(p));
918 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
919 addq(&kbdcp(p)->kbd, b);
933 if ((c1 > 0) || (c2 > 0))
934 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
937 emptyq(&kbdcp(p)->aux);
940 /* discard any data from the keyboard or the aux device */
942 empty_both_buffers(KBDC p, int wait)
953 for (t = wait; t > 0; ) {
954 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
955 DELAY(KBDD_DELAYTIME);
956 (void)read_data(kbdcp(p));
958 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
969 * Some systems (Intel/IBM blades) do not have keyboard devices and
970 * will thus hang in this procedure. Time out after delta seconds to
971 * avoid this hang -- the keyboard attach will fail later on.
973 waited += (delta * 1000);
974 if (waited == (delta * 1000000))
980 if ((c1 > 0) || (c2 > 0))
981 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
984 emptyq(&kbdcp(p)->kbd);
985 emptyq(&kbdcp(p)->aux);
988 /* keyboard and mouse device control */
990 /* NOTE: enable the keyboard port but disable the keyboard
991 * interrupt before calling "reset_kbd()".
996 int retry = KBD_MAXRETRY;
997 int again = KBD_MAXWAIT;
998 int c = KBD_RESEND; /* keep the compiler happy */
1000 while (retry-- > 0) {
1001 empty_both_buffers(p, 10);
1002 if (!write_kbd_command(p, KBDC_RESET_KBD))
1004 emptyq(&kbdcp(p)->kbd);
1005 c = read_controller_data(p);
1006 if (verbose || bootverbose)
1007 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
1008 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
1014 while (again-- > 0) {
1015 /* wait awhile, well, in fact we must wait quite loooooooooooong */
1016 DELAY(KBD_RESETDELAY*1000);
1017 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
1018 if (c != -1) /* wait again if the controller is not ready */
1021 if (verbose || bootverbose)
1022 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
1023 if (c != KBD_RESET_DONE)
1028 /* NOTE: enable the aux port but disable the aux interrupt
1029 * before calling `reset_aux_dev()'.
1032 reset_aux_dev(KBDC p)
1034 int retry = KBD_MAXRETRY;
1035 int again = KBD_MAXWAIT;
1036 int c = PSM_RESEND; /* keep the compiler happy */
1038 while (retry-- > 0) {
1039 empty_both_buffers(p, 10);
1040 if (!write_aux_command(p, PSMC_RESET_DEV))
1042 emptyq(&kbdcp(p)->aux);
1043 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
1044 for (again = KBD_MAXWAIT; again > 0; --again) {
1045 DELAY(KBD_RESETDELAY*1000);
1046 c = read_aux_data_no_wait(p);
1050 if (verbose || bootverbose)
1051 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
1052 if (c == PSM_ACK) /* aux dev is about to reset... */
1058 for (again = KBD_MAXWAIT; again > 0; --again) {
1059 /* wait awhile, well, quite looooooooooooong */
1060 DELAY(KBD_RESETDELAY*1000);
1061 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
1062 if (c != -1) /* wait again if the controller is not ready */
1065 if (verbose || bootverbose)
1066 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
1067 if (c != PSM_RESET_DONE) /* reset status */
1070 c = read_aux_data(p); /* device ID */
1071 if (verbose || bootverbose)
1072 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
1073 /* NOTE: we could check the device ID now, but leave it later... */
1077 /* controller diagnostics and setup */
1080 test_controller(KBDC p)
1082 int retry = KBD_MAXRETRY;
1083 int again = KBD_MAXWAIT;
1084 int c = KBD_DIAG_FAIL;
1086 while (retry-- > 0) {
1087 empty_both_buffers(p, 10);
1088 if (write_controller_command(p, KBDC_DIAGNOSE))
1094 emptyq(&kbdcp(p)->kbd);
1095 while (again-- > 0) {
1097 DELAY(KBD_RESETDELAY*1000);
1098 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
1099 if (c != -1) /* wait again if the controller is not ready */
1102 if (verbose || bootverbose)
1103 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
1104 return (c == KBD_DIAG_DONE);
1108 test_kbd_port(KBDC p)
1110 int retry = KBD_MAXRETRY;
1111 int again = KBD_MAXWAIT;
1114 while (retry-- > 0) {
1115 empty_both_buffers(p, 10);
1116 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1122 emptyq(&kbdcp(p)->kbd);
1123 while (again-- > 0) {
1124 c = read_controller_data(p);
1125 if (c != -1) /* try again if the controller is not ready */
1128 if (verbose || bootverbose)
1129 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1134 test_aux_port(KBDC p)
1136 int retry = KBD_MAXRETRY;
1137 int again = KBD_MAXWAIT;
1140 while (retry-- > 0) {
1141 empty_both_buffers(p, 10);
1142 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1148 emptyq(&kbdcp(p)->kbd);
1149 while (again-- > 0) {
1150 c = read_controller_data(p);
1151 if (c != -1) /* try again if the controller is not ready */
1154 if (verbose || bootverbose)
1155 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1160 kbdc_get_device_mask(KBDC p)
1162 return kbdcp(p)->command_mask;
1166 kbdc_set_device_mask(KBDC p, int mask)
1168 kbdcp(p)->command_mask =
1169 mask & (((kbdcp(p)->quirks & KBDC_QUIRK_KEEP_ACTIVATED)
1170 ? 0 : KBD_KBD_CONTROL_BITS) | KBD_AUX_CONTROL_BITS);
1174 get_controller_command_byte(KBDC p)
1176 if (kbdcp(p)->command_byte != -1)
1177 return kbdcp(p)->command_byte;
1178 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1180 emptyq(&kbdcp(p)->kbd);
1181 kbdcp(p)->command_byte = read_controller_data(p);
1182 return kbdcp(p)->command_byte;
1186 set_controller_command_byte(KBDC p, int mask, int command)
1188 if (get_controller_command_byte(p) == -1)
1191 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1192 if (command & KBD_DISABLE_KBD_PORT) {
1193 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1196 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1198 if (!write_controller_data(p, command))
1200 kbdcp(p)->command_byte = command;
1203 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",
1210 * Rudimentary support for active PS/2 AUX port multiplexing.
1211 * Only write commands can be routed to a selected AUX port.
1212 * Source port of data processed by read commands is totally ignored.
1215 set_aux_mux_state(KBDC p, int enabled)
1217 int command, version;
1219 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1220 write_controller_data(p, 0xF0) == 0 ||
1221 read_controller_data(p) != 0xF0)
1224 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1225 write_controller_data(p, 0x56) == 0 ||
1226 read_controller_data(p) != 0x56)
1229 command = enabled ? 0xa4 : 0xa5;
1230 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 ||
1231 write_controller_data(p, command) == 0 ||
1232 (version = read_controller_data(p)) == command)
1239 set_active_aux_mux_port(KBDC p, int port)
1242 if (!aux_mux_is_enabled(p))
1245 if (port < 0 || port >= KBDC_AUX_MUX_NUM_PORTS)
1248 kbdcp(p)->aux_mux_port = port;
1253 /* Checks for active multiplexing support and enables it */
1255 enable_aux_mux(KBDC p)
1259 version = set_aux_mux_state(p, TRUE);
1261 kbdcp(p)->aux_mux_enabled = TRUE;
1262 set_active_aux_mux_port(p, 0);
1269 disable_aux_mux(KBDC p)
1272 kbdcp(p)->aux_mux_enabled = FALSE;
1274 return (set_aux_mux_state(p, FALSE));
1278 aux_mux_is_enabled(KBDC p)
1281 return (kbdcp(p)->aux_mux_enabled);