4 * Copyright (c) 1996-1999
5 * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/dev/kbd/atkbdc.c,v 1.5.2.2 2002/03/31 11:02:02 murray Exp $
33 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/syslog.h>
47 #include <machine/clock.h>
49 #include <bus/isa/isareg.h>
51 #include "atkbdcreg.h"
53 #include "use_atkbdc.h"
57 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
62 #define MAX(x, y) ((x) > (y) ? (x) : (y))
65 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
66 #define availq(q) ((q)->head != (q)->tail)
68 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
70 #define emptyq(q) ((q)->tail = (q)->head = 0)
73 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
74 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
75 #define write_data(k, d) (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
76 #define write_command(k, d) (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
81 * We always need at least one copy of the kbdc_softc struct for the
82 * low-level console. As the low-level console accesses the keyboard
83 * controller before kbdc, and all other devices, is probed, we
84 * statically allocate one entry. XXX
86 static atkbdc_softc_t default_kbdc
;
87 static atkbdc_softc_t
*atkbdc_softc
[MAXKBDC
] = { &default_kbdc
};
89 static int verbose
= KBDIO_DEBUG
;
91 /* function prototypes */
93 static int atkbdc_setup(atkbdc_softc_t
*sc
, bus_space_tag_t tag
,
94 bus_space_handle_t h0
, bus_space_handle_t h1
);
95 static int addq(kbdkqueue
*q
, int c
);
96 static int removeq(kbdkqueue
*q
);
97 static int wait_while_controller_busy(atkbdc_softc_t
*kbdc
);
98 static int wait_for_data(atkbdc_softc_t
*kbdc
);
99 static int wait_for_kbd_data(atkbdc_softc_t
*kbdc
);
100 static int wait_for_kbd_ack(atkbdc_softc_t
*kbdc
);
101 static int wait_for_aux_data(atkbdc_softc_t
*kbdc
);
102 static int wait_for_aux_ack(atkbdc_softc_t
*kbdc
);
104 struct atkbdc_quirks
{
105 const char *bios_vendor
;
112 /* Old chromebooks running coreboot with i8042 emulation quirks */
113 #define CHROMEBOOK_WORKAROUND \
114 (KBDC_QUIRK_KEEP_ACTIVATED | KBDC_QUIRK_IGNORE_PROBE_RESULT | \
115 KBDC_QUIRK_RESET_AFTER_PROBE | KBDC_QUIRK_SETLEDS_ON_INIT)
117 static struct atkbdc_quirks quirks
[] = {
119 * Older chromebooks running coreboot have an EC that imperfectly
120 * emulates i8042 w/o fixes to its firmware. Since we can't probe for
121 * the problem, include all chromebooks by matching 'Google_' in the
122 * bios version string or a maker of either 'Google' or 'GOOGLE'. This
123 * is imperfect, but catches all chromebooks while omitting non-Google
124 * systems from System76 and Purism.
126 { "coreboot", NULL
, NULL
, "Google_", CHROMEBOOK_WORKAROUND
},
127 { "coreboot", "GOOGLE", NULL
, NULL
, CHROMEBOOK_WORKAROUND
},
128 { "coreboot", "Google", NULL
, NULL
, CHROMEBOOK_WORKAROUND
},
129 /* KBDC hangs on Lenovo X120e and X121e after disabling AUX MUX */
130 { NULL
, "LENOVO", NULL
, NULL
, KBDC_QUIRK_DISABLE_MUX_PROBE
},
133 #define QUIRK_STR_EQUAL(s1, s2) \
134 (s1 == NULL || (s2 != NULL && strcmp(s1, s2) == 0))
135 #define QUIRK_STR_MATCH(s1, s2) \
136 (s1 == NULL || (s2 != NULL && strncmp(s1, s2, strlen(s1)) == 0))
139 atkbdc_getquirks(void)
142 char *bios_vendor
= kgetenv("smbios.bios.vendor");
143 char *maker
= kgetenv("smbios.system.maker");
144 char *product
= kgetenv("smbios.system.product");
145 char *version
= kgetenv("smbios.bios.version");
146 char *reldate
= kgetenv("smbios.bios.reldate");
148 for (i
= 0; i
< nitems(quirks
); i
++)
149 if (QUIRK_STR_EQUAL(quirks
[i
].bios_vendor
, bios_vendor
) &&
150 QUIRK_STR_EQUAL(quirks
[i
].maker
, maker
) &&
151 QUIRK_STR_EQUAL(quirks
[i
].product
, product
) &&
152 QUIRK_STR_MATCH(quirks
[i
].version
, version
))
153 return (quirks
[i
].quirk
);
155 * Some Chromebooks don't conform to the google comment above so do the
156 * Chromebook workaround for all <= 2018 coreboot systems that have a
157 * 'blank' version. At least one Acer "Peppy" chromebook has this
158 * issue, with a reldate of 08/13/2014.
160 if (QUIRK_STR_EQUAL("coreboot", bios_vendor
) &&
161 (version
!= NULL
&& *version
== ' ') &&
162 (reldate
!= NULL
&& strlen(reldate
) >= 10 &&
163 strcmp(reldate
+ 6, "2018") <= 0))
164 return (CHROMEBOOK_WORKAROUND
);
170 atkbdc_get_softc(int unit
)
174 if (unit
>= nitems(atkbdc_softc
))
176 sc
= atkbdc_softc
[unit
];
178 sc
= kmalloc(sizeof(*sc
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
179 atkbdc_softc
[unit
] = sc
;
185 atkbdc_probe_unit(int unit
, struct resource
*port0
, struct resource
*port1
)
187 if (rman_get_start(port0
) <= 0)
189 if (rman_get_start(port1
) <= 0)
195 atkbdc_attach_unit(int unit
, atkbdc_softc_t
*sc
, struct resource
*port0
,
196 struct resource
*port1
)
198 return atkbdc_setup(sc
, rman_get_bustag(port0
),
199 rman_get_bushandle(port0
), rman_get_bushandle(port1
));
202 extern int acpi_fadt_8042_nolegacy
;
203 int kicked_by_syscons
= 0;
206 atkbdc_fadt_done(void)
208 if (kicked_by_syscons
) {
210 * Configuring all keyboards is fine, because only atkbd
211 * really does something here, anyway.
213 kbd_configure(KB_CONF_PROBE_ONLY
);
217 /* After fadt_probe in platform/pc64/acpica/acpi_fadt.c. */
218 SYSINIT(atkbdc_kick
, SI_BOOT2_PRESMP
, SI_ORDER_THIRD
, atkbdc_fadt_done
, 0);
220 /* the backdoor to the keyboard controller! XXX */
222 atkbdc_configure(void)
225 bus_space_handle_t h0
;
226 bus_space_handle_t h1
;
229 #if defined(__x86_64__)
233 kicked_by_syscons
= 1;
234 if (acpi_fadt_8042_nolegacy
!= 0)
238 resource_int_value("atkbdc", 0, "port", &port0
);
239 port1
= IO_KBD
+ KBD_STATUS_PORT
;
241 resource_int_value("atkbdc", 0, "port", &port0
);
244 /* XXX: tag should be passed from the caller */
245 #if defined(__x86_64__)
246 tag
= X86_64_BUS_SPACE_IO
;
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
;
259 #if defined(__x86_64__)
261 * Check if we really have AT keyboard controller. Poll status
262 * register until we get "all clear" indication. If no such
263 * indication comes, it probably means that there is no AT
264 * keyboard controller present. Give up in such case. Check relies
265 * on the fact that reading from non-existing in/out port returns
266 * 0xff on i386. May or may not be true on other platforms.
268 for (i
= 65536; i
!= 0; --i
) {
269 if ((bus_space_read_1(tag
, h1
, 0) & 0x2) == 0)
277 return atkbdc_setup(atkbdc_softc
[0], tag
, h0
, h1
);
281 atkbdc_setup(atkbdc_softc_t
*sc
, bus_space_tag_t tag
, bus_space_handle_t h0
,
282 bus_space_handle_t h1
)
284 u_int64_t tscval
[3], read_delay
;
287 if (sc
->ioh0
== 0) { /* XXX */
288 sc
->command_byte
= -1;
289 sc
->command_mask
= 0;
291 sc
->kbd
.head
= sc
->kbd
.tail
= 0;
292 sc
->aux
.head
= sc
->aux
.tail
= 0;
293 sc
->aux_mux_enabled
= FALSE
;
295 sc
->kbd
.call_count
= 0;
296 sc
->kbd
.qcount
= sc
->kbd
.max_qcount
= 0;
297 sc
->aux
.call_count
= 0;
298 sc
->aux
.qcount
= sc
->aux
.max_qcount
= 0;
305 * On certain chipsets AT keyboard controller isn't present and is
306 * emulated by BIOS using SMI interrupt. On those chipsets reading
307 * from the status port may be thousand times slower than usually.
308 * Sometimes this emilation is not working properly resulting in
309 * commands timing our and since we assume that inb() operation
310 * takes very little time to complete we need to adjust number of
311 * retries to keep waiting time within a designed limits (100ms).
312 * Measure time it takes to make read_status() call and adjust
313 * number of retries accordingly.
315 flags
= intr_disable();
322 read_delay
= tscval
[1] - tscval
[0];
323 read_delay
/= (tscval
[2] - tscval
[1]) / 1000;
324 sc
->retry
= 100000 / ((KBDD_DELAYTIME
* 2) + read_delay
);
326 sc
->quirks
= atkbdc_getquirks();
331 /* open a keyboard controller */
333 atkbdc_open(int unit
)
339 if ((atkbdc_softc
[unit
]->port0
!= NULL
) ||
340 (atkbdc_softc
[unit
]->ioh0
!= 0)) /* XXX */
341 return (KBDC
)atkbdc_softc
[unit
];
346 * I/O access arbitration in `kbdio'
348 * The `kbdio' module uses a simplistic convention to arbitrate
349 * I/O access to the controller/keyboard/mouse. The convention requires
350 * close cooperation of the calling device driver.
352 * The device drivers which utilize the `kbdio' module are assumed to
353 * have the following set of routines.
354 * a. An interrupt handler (the bottom half of the driver).
355 * b. Timeout routines which may briefly poll the keyboard controller.
356 * c. Routines outside interrupt context (the top half of the driver).
357 * They should follow the rules below:
358 * 1. The interrupt handler may assume that it always has full access
359 * to the controller/keyboard/mouse.
360 * 2. The other routines must issue `spltty()' if they wish to
361 * prevent the interrupt handler from accessing
362 * the controller/keyboard/mouse.
363 * 3. The timeout routines and the top half routines of the device driver
364 * arbitrate I/O access by observing the lock flag in `kbdio'.
365 * The flag is manipulated via `kbdc_lock()'; when one wants to
366 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
367 * the call returns with TRUE. Otherwise the caller must back off.
368 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
369 * is finished. This mechanism does not prevent the interrupt
370 * handler from being invoked at any time and carrying out I/O.
371 * Therefore, `spltty()' must be strategically placed in the device
372 * driver code. Also note that the timeout routine may interrupt
373 * `kbdc_lock()' called by the top half of the driver, but this
374 * interruption is OK so long as the timeout routine observes
376 * 4. The interrupt and timeout routines should not extend I/O operation
377 * across more than one interrupt or timeout; they must complete any
378 * necessary I/O operation within one invocation of the routine.
379 * This means that if the timeout routine acquires the lock flag,
380 * it must reset the flag to FALSE before it returns.
383 /* set/reset polling lock */
385 kbdc_lock(KBDC p
, int lock
)
392 return (prevlock
!= lock
);
395 /* check if any data is waiting to be processed */
397 kbdc_data_ready(KBDC p
)
399 return (availq(&p
->kbd
) || availq(&p
->aux
) ||
400 (read_status(p
) & KBDS_ANY_BUFFER_FULL
));
403 /* queuing functions */
406 addq(kbdkqueue
*q
, int c
)
408 if (nextq(q
->tail
) != q
->head
) {
410 q
->tail
= nextq(q
->tail
);
414 if (q
->qcount
> q
->max_qcount
)
415 q
->max_qcount
= q
->qcount
;
423 removeq(kbdkqueue
*q
)
427 if (q
->tail
!= q
->head
) {
429 q
->head
= nextq(q
->head
);
439 * device I/O routines
442 wait_while_controller_busy(struct atkbdc_softc
*kbdc
)
444 /* CPU will stay inside the loop for 100msec at most */
445 TOTALDELAY retry
= { .us
= 70000, .last_clock
= 0 }; /* 70ms */
449 while ((f
= read_status(kbdc
)) & KBDS_INPUT_BUFFER_FULL
) {
450 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
451 DELAY(KBDD_DELAYTIME
);
454 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
455 DELAY(KBDD_DELAYTIME
);
459 DELAY(KBDC_DELAYTIME
);
460 if (CHECKTIMEOUT(&retry
))
467 * wait for any data; whether it's from the controller,
468 * the keyboard, or the aux device.
471 wait_for_data(struct atkbdc_softc
*kbdc
)
473 /* CPU will stay inside the loop for 200msec at most */
474 TOTALDELAY retry
= { 200000, 0 }; /* 200ms */
477 while ((f
= read_status(kbdc
) & KBDS_ANY_BUFFER_FULL
) == 0) {
478 DELAY(KBDC_DELAYTIME
);
479 if (CHECKTIMEOUT(&retry
))
482 DELAY(KBDD_DELAYTIME
);
486 /* wait for data from the keyboard */
488 wait_for_kbd_data(struct atkbdc_softc
*kbdc
)
490 /* CPU will stay inside the loop for 200msec at most */
491 TOTALDELAY retry
= { 200000, 0 }; /* 200ms */
495 while ((f
= read_status(kbdc
) & KBDS_BUFFER_FULL
) !=
496 KBDS_KBD_BUFFER_FULL
) {
497 if (f
== KBDS_AUX_BUFFER_FULL
) {
498 DELAY(KBDD_DELAYTIME
);
502 DELAY(KBDC_DELAYTIME
);
503 if (CHECKTIMEOUT(&retry
))
506 DELAY(KBDD_DELAYTIME
);
511 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
512 * queue anything else.
515 wait_for_kbd_ack(struct atkbdc_softc
*kbdc
)
517 /* CPU will stay inside the loop for 200msec at most */
518 TOTALDELAY retry
= { 200000, 0 }; /* 200ms */
522 while (CHECKTIMEOUT(&retry
) == 0) {
523 if ((f
= read_status(kbdc
)) & KBDS_ANY_BUFFER_FULL
) {
524 DELAY(KBDD_DELAYTIME
);
526 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
527 if ((b
== KBD_ACK
) || (b
== KBD_RESEND
) ||
528 (b
== KBD_RESET_FAIL
))
531 } else if ((f
& KBDS_BUFFER_FULL
) ==
532 KBDS_AUX_BUFFER_FULL
) {
536 DELAY(KBDC_DELAYTIME
);
541 /* wait for data from the aux device */
543 wait_for_aux_data(struct atkbdc_softc
*kbdc
)
545 /* CPU will stay inside the loop for 200msec at most */
546 TOTALDELAY retry
= { 200000, 0 }; /* 200ms */
550 while ((f
= read_status(kbdc
) & KBDS_BUFFER_FULL
) !=
551 KBDS_AUX_BUFFER_FULL
) {
552 if (f
== KBDS_KBD_BUFFER_FULL
) {
553 DELAY(KBDD_DELAYTIME
);
557 DELAY(KBDC_DELAYTIME
);
558 if (CHECKTIMEOUT(&retry
))
561 DELAY(KBDD_DELAYTIME
);
566 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
567 * queue anything else.
570 wait_for_aux_ack(struct atkbdc_softc
*kbdc
)
572 /* CPU will stay inside the loop for 200msec at most */
573 TOTALDELAY retry
= { 200000, 0 }; /* 200ms */
577 while (CHECKTIMEOUT(&retry
) == 0) {
578 if ((f
= read_status(kbdc
)) & KBDS_ANY_BUFFER_FULL
) {
579 DELAY(KBDD_DELAYTIME
);
581 if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
582 if ((b
== PSM_ACK
) || (b
== PSM_RESEND
) ||
583 (b
== PSM_RESET_FAIL
))
586 } else if ((f
& KBDS_BUFFER_FULL
) ==
587 KBDS_KBD_BUFFER_FULL
) {
591 DELAY(KBDC_DELAYTIME
);
597 * Returns read-back data or -1 on failure
600 write_controller_w1r1(KBDC p
, int c
, int d
)
602 if (!write_controller_command(p
, c
))
604 if (!write_controller_data(p
, d
))
606 return (read_controller_data(p
));
609 /* write a one byte command to the controller */
611 write_controller_command(KBDC p
, int c
)
613 if (!wait_while_controller_busy(p
))
619 /* write a one byte data to the controller */
621 write_controller_data(KBDC p
, int c
)
623 if (!wait_while_controller_busy(p
))
629 /* write a one byte keyboard command */
631 write_kbd_command(KBDC p
, int c
)
633 if (!wait_while_controller_busy(p
))
639 /* write a one byte auxiliary device command */
641 write_aux_command(KBDC p
, int c
)
645 f
= aux_mux_is_enabled(p
) ? KBDC_WRITE_TO_AUX_MUX
+ p
->aux_mux_port
:
648 if (!write_controller_command(p
, f
))
650 return write_controller_data(p
, c
);
653 /* send a command to the keyboard and wait for ACK */
655 send_kbd_command(KBDC p
, int c
)
657 int retry
= KBD_MAXRETRY
;
660 while (retry
-- > 0) {
661 if (!write_kbd_command(p
, c
))
663 res
= wait_for_kbd_ack(p
);
670 /* send a command to the auxiliary device and wait for ACK */
672 send_aux_command(KBDC p
, int c
)
674 int retry
= KBD_MAXRETRY
;
677 while (retry
-- > 0) {
678 if (!write_aux_command(p
, c
))
682 * The aux device may have already sent one or two bytes of
683 * status data, when a command is received. It will immediately
684 * stop data transmission, thus, leaving an incomplete data
685 * packet in our buffer. We have to discard any unprocessed
686 * data in order to remove such packets. Well, we may remove
687 * unprocessed, but necessary data byte as well...
690 res
= wait_for_aux_ack(p
);
697 /* send a command and a data to the keyboard, wait for ACKs */
699 send_kbd_command_and_data(KBDC p
, int c
, int d
)
704 for (retry
= KBD_MAXRETRY
; retry
> 0; --retry
) {
705 if (!write_kbd_command(p
, c
))
707 res
= wait_for_kbd_ack(p
);
710 else if (res
!= KBD_RESEND
)
716 for (retry
= KBD_MAXRETRY
, res
= -1; retry
> 0; --retry
) {
717 if (!write_kbd_command(p
, d
))
719 res
= wait_for_kbd_ack(p
);
720 if (res
!= KBD_RESEND
)
726 /* send a command and a data to the auxiliary device, wait for ACKs */
728 send_aux_command_and_data(KBDC p
, int c
, int d
)
733 for (retry
= KBD_MAXRETRY
; retry
> 0; --retry
) {
734 if (!write_aux_command(p
, c
))
737 res
= wait_for_aux_ack(p
);
740 else if (res
!= PSM_RESEND
)
746 for (retry
= KBD_MAXRETRY
, res
= -1; retry
> 0; --retry
) {
747 if (!write_aux_command(p
, d
))
749 res
= wait_for_aux_ack(p
);
750 if (res
!= PSM_RESEND
)
757 * read one byte from any source; whether from the controller,
758 * the keyboard, or the aux device
761 read_controller_data(KBDC p
)
764 return removeq(&p
->kbd
);
766 return removeq(&p
->aux
);
767 if (!wait_for_data(p
))
768 return -1; /* timeout */
776 /* read one byte from the keyboard */
778 read_kbd_data(KBDC p
)
786 "kbdc: kbd q: %d calls, max %d chars, "
787 "aux q: %d calls, max %d chars\n",
788 p
->kbd
.call_count
, p
->kbd
.max_qcount
, p
->aux
.call_count
,
794 return removeq(&p
->kbd
);
795 if (!wait_for_kbd_data(p
))
796 return -1; /* timeout */
801 /* read one byte from the keyboard, but return immediately if
805 read_kbd_data_no_wait(KBDC p
)
814 "kbdc: kbd q: %d calls, max %d chars, "
815 "aux q: %d calls, max %d chars\n",
816 p
->kbd
.call_count
, p
->kbd
.max_qcount
, p
->aux
.call_count
,
822 return removeq(&p
->kbd
);
823 f
= read_status(p
) & KBDS_BUFFER_FULL
;
824 while (f
== KBDS_AUX_BUFFER_FULL
) {
825 DELAY(KBDD_DELAYTIME
);
828 f
= read_status(p
) & KBDS_BUFFER_FULL
;
830 if (f
== KBDS_KBD_BUFFER_FULL
) {
831 DELAY(KBDD_DELAYTIME
);
835 return -1; /* no data */
838 /* read one byte from the aux device */
840 read_aux_data(KBDC p
)
844 return removeq(&p
->aux
);
845 if (!wait_for_aux_data(p
))
846 return -1; /* timeout */
851 /* read one byte from the aux device, but return immediately if
855 read_aux_data_no_wait(KBDC p
)
861 return removeq(&p
->aux
);
862 f
= read_status(p
) & KBDS_BUFFER_FULL
;
863 while (f
== KBDS_KBD_BUFFER_FULL
) {
864 DELAY(KBDD_DELAYTIME
);
867 f
= read_status(p
) & KBDS_BUFFER_FULL
;
869 if (f
== KBDS_AUX_BUFFER_FULL
) {
870 DELAY(KBDD_DELAYTIME
);
874 return -1; /* no data */
877 /* discard data from the keyboard */
879 empty_kbd_buffer(KBDC p
, int wait
)
890 for (t
= wait
; t
> 0;) {
891 if ((f
= read_status(p
)) & KBDS_ANY_BUFFER_FULL
) {
892 DELAY(KBDD_DELAYTIME
);
894 if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
909 if ((c1
> 0) || (c2
> 0))
910 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1
,
917 /* discard data from the aux device */
919 empty_aux_buffer(KBDC p
, int wait
)
930 for (t
= wait
; t
> 0;) {
931 if ((f
= read_status(p
)) & KBDS_ANY_BUFFER_FULL
) {
932 DELAY(KBDD_DELAYTIME
);
934 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
949 if ((c1
> 0) || (c2
> 0))
950 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1
,
957 /* discard any data from the keyboard or the aux device */
959 empty_both_buffers(KBDC p
, int wait
)
970 for (t
= wait
; t
> 0;) {
971 if ((f
= read_status(p
)) & KBDS_ANY_BUFFER_FULL
) {
972 DELAY(KBDD_DELAYTIME
);
975 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
)
985 * Some systems (Intel/IBM blades) do not have keyboard devices and
986 * will thus hang in this procedure. Time out after delta seconds to
987 * avoid this hang -- the keyboard attach will fail later on.
989 waited
+= (delta
* 1000);
990 if (waited
== (delta
* 1000000))
996 if ((c1
> 0) || (c2
> 0))
997 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_both_buffers)\n",
1005 /* keyboard and mouse device control */
1007 /* NOTE: enable the keyboard port but disable the keyboard
1008 * interrupt before calling "reset_kbd()".
1013 int retry
= KBD_MAXRETRY
;
1014 int again
= KBD_MAXWAIT
;
1015 int c
= KBD_RESEND
; /* keep the compiler happy */
1017 while (retry
-- > 0) {
1018 empty_both_buffers(p
, 10);
1019 if (!write_kbd_command(p
, KBDC_RESET_KBD
))
1022 c
= read_controller_data(p
);
1023 if (verbose
|| bootverbose
)
1024 log(LOG_DEBUG
, "kbdc: RESET_KBD return code:%04x\n", c
);
1025 if (c
== KBD_ACK
) /* keyboard has agreed to reset itself... */
1031 while (again
-- > 0) {
1032 /* wait awhile, well, in fact we must wait quite loooooooooooong
1034 DELAY(KBD_RESETDELAY
* 1000);
1035 c
= read_controller_data(p
); /* RESET_DONE/RESET_FAIL */
1036 if (c
!= -1) /* wait again if the controller is not ready */
1039 if (verbose
|| bootverbose
)
1040 log(LOG_DEBUG
, "kbdc: RESET_KBD status:%04x\n", c
);
1041 if (c
!= KBD_RESET_DONE
)
1046 /* NOTE: enable the aux port but disable the aux interrupt
1047 * before calling `reset_aux_dev()'.
1050 reset_aux_dev(KBDC p
)
1052 int retry
= KBD_MAXRETRY
;
1053 int again
= KBD_MAXWAIT
;
1054 int c
= PSM_RESEND
; /* keep the compiler happy */
1056 while (retry
-- > 0) {
1057 empty_both_buffers(p
, 10);
1058 if (!write_aux_command(p
, PSMC_RESET_DEV
))
1061 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
1062 for (again
= KBD_MAXWAIT
; again
> 0; --again
) {
1063 DELAY(KBD_RESETDELAY
* 1000);
1064 c
= read_aux_data_no_wait(p
);
1068 if (verbose
|| bootverbose
)
1069 log(LOG_DEBUG
, "kbdc: RESET_AUX return code:%04x\n", c
);
1070 if (c
== PSM_ACK
) /* aux dev is about to reset... */
1076 for (again
= KBD_MAXWAIT
; again
> 0; --again
) {
1077 /* wait awhile, well, quite looooooooooooong */
1078 DELAY(KBD_RESETDELAY
* 1000);
1079 c
= read_aux_data_no_wait(p
); /* RESET_DONE/RESET_FAIL */
1080 if (c
!= -1) /* wait again if the controller is not ready */
1083 if (verbose
|| bootverbose
)
1084 log(LOG_DEBUG
, "kbdc: RESET_AUX status:%04x\n", c
);
1085 if (c
!= PSM_RESET_DONE
) /* reset status */
1088 c
= read_aux_data(p
); /* device ID */
1089 if (verbose
|| bootverbose
)
1090 log(LOG_DEBUG
, "kbdc: RESET_AUX ID:%04x\n", c
);
1091 /* NOTE: we could check the device ID now, but leave it later... */
1095 /* controller diagnostics and setup */
1098 test_controller(KBDC p
)
1100 int retry
= KBD_MAXRETRY
;
1101 int again
= KBD_MAXWAIT
;
1102 int c
= KBD_DIAG_FAIL
;
1104 while (retry
-- > 0) {
1105 empty_both_buffers(p
, 10);
1106 if (write_controller_command(p
, KBDC_DIAGNOSE
))
1113 while (again
-- > 0) {
1115 DELAY(KBD_RESETDELAY
* 1000);
1116 c
= read_controller_data(p
); /* DIAG_DONE/DIAG_FAIL */
1117 if (c
!= -1) /* wait again if the controller is not ready */
1120 if (verbose
|| bootverbose
)
1121 log(LOG_DEBUG
, "kbdc: DIAGNOSE status:%04x\n", c
);
1122 return (c
== KBD_DIAG_DONE
);
1126 test_kbd_port(KBDC p
)
1128 int retry
= KBD_MAXRETRY
;
1129 int again
= KBD_MAXWAIT
;
1132 while (retry
-- > 0) {
1133 empty_both_buffers(p
, 10);
1134 if (write_controller_command(p
, KBDC_TEST_KBD_PORT
))
1141 while (again
-- > 0) {
1142 c
= read_controller_data(p
);
1143 if (c
!= -1) /* try again if the controller is not ready */
1146 if (verbose
|| bootverbose
)
1147 log(LOG_DEBUG
, "kbdc: TEST_KBD_PORT status:%04x\n", c
);
1152 test_aux_port(KBDC p
)
1154 int retry
= KBD_MAXRETRY
;
1155 int again
= KBD_MAXWAIT
;
1158 while (retry
-- > 0) {
1159 empty_both_buffers(p
, 10);
1160 if (write_controller_command(p
, KBDC_TEST_AUX_PORT
))
1167 while (again
-- > 0) {
1168 c
= read_controller_data(p
);
1169 if (c
!= -1) /* try again if the controller is not ready */
1172 if (verbose
|| bootverbose
)
1173 log(LOG_DEBUG
, "kbdc: TEST_AUX_PORT status:%04x\n", c
);
1178 kbdc_get_device_mask(KBDC p
)
1180 return p
->command_mask
;
1184 kbdc_set_device_mask(KBDC p
, int mask
)
1186 p
->command_mask
= mask
&
1187 (((p
->quirks
& KBDC_QUIRK_KEEP_ACTIVATED
) ? 0 :
1188 KBD_KBD_CONTROL_BITS
) |
1189 KBD_AUX_CONTROL_BITS
);
1193 get_controller_command_byte(KBDC p
)
1195 if (p
->command_byte
!= -1)
1196 return p
->command_byte
;
1197 if (!write_controller_command(p
, KBDC_GET_COMMAND_BYTE
))
1200 p
->command_byte
= read_controller_data(p
);
1201 return p
->command_byte
;
1205 set_controller_command_byte(KBDC p
, int mask
, int command
)
1207 if (get_controller_command_byte(p
) == -1)
1210 command
= (p
->command_byte
& ~mask
) | (command
& mask
);
1211 if (command
& KBD_DISABLE_KBD_PORT
) {
1212 if (!write_controller_command(p
, KBDC_DISABLE_KBD_PORT
))
1215 if (!write_controller_command(p
, KBDC_SET_COMMAND_BYTE
))
1217 if (!write_controller_data(p
, command
))
1219 p
->command_byte
= command
;
1223 "kbdc: new command byte:%04x (set_controller...)\n",
1230 * Rudimentary support for active PS/2 AUX port multiplexing.
1231 * Only write commands can be routed to a selected AUX port.
1232 * Source port of data processed by read commands is totally ignored.
1235 set_aux_mux_state(KBDC p
, int enabled
)
1237 int command
, version
;
1239 if (write_controller_command(p
, KBDC_FORCE_AUX_OUTPUT
) == 0 ||
1240 write_controller_data(p
, 0xF0) == 0 ||
1241 read_controller_data(p
) != 0xF0)
1244 if (write_controller_command(p
, KBDC_FORCE_AUX_OUTPUT
) == 0 ||
1245 write_controller_data(p
, 0x56) == 0 ||
1246 read_controller_data(p
) != 0x56)
1249 command
= enabled
? 0xa4 : 0xa5;
1250 if (write_controller_command(p
, KBDC_FORCE_AUX_OUTPUT
) == 0 ||
1251 write_controller_data(p
, command
) == 0 ||
1252 (version
= read_controller_data(p
)) == command
)
1259 set_active_aux_mux_port(KBDC p
, int port
)
1262 if (!aux_mux_is_enabled(p
))
1265 if (port
< 0 || port
>= KBDC_AUX_MUX_NUM_PORTS
)
1268 p
->aux_mux_port
= port
;
1273 /* Checks for active multiplexing support and enables it */
1275 enable_aux_mux(KBDC p
)
1279 version
= set_aux_mux_state(p
, TRUE
);
1281 p
->aux_mux_enabled
= TRUE
;
1282 set_active_aux_mux_port(p
, 0);
1289 disable_aux_mux(KBDC p
)
1292 p
->aux_mux_enabled
= FALSE
;
1294 return (set_aux_mux_state(p
, FALSE
));
1298 aux_mux_is_enabled(KBDC p
)
1301 return (p
->aux_mux_enabled
);