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 * $FreeBSD: src/sys/dev/kbd/atkbdc.c,v 1.5.2.2 2002/03/31 11:02:02 murray Exp $
31 * $DragonFly: src/sys/dev/misc/kbd/atkbdc.c,v 1.4 2004/05/13 19:44:32 dillon Exp $
32 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
36 #include "use_atkbdc.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/syslog.h>
43 #include <machine/bus_pio.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
48 #include <machine/clock.h>
50 #include "atkbdcreg.h"
52 #include <bus/isa/isareg.h>
56 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
61 #define MAX(x, y) ((x) > (y) ? (x) : (y))
64 #define kbdcp(p) ((atkbdc_softc_t *)(p))
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) \
76 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
77 #define write_command(k, d) \
78 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
83 * We always need at least one copy of the kbdc_softc struct for the
84 * low-level console. As the low-level console accesses the keyboard
85 * controller before kbdc, and all other devices, is probed, we
86 * statically allocate one entry. XXX
88 static atkbdc_softc_t default_kbdc;
89 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
91 static int verbose = KBDIO_DEBUG;
93 /* function prototypes */
95 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
96 bus_space_handle_t h0, bus_space_handle_t h1);
97 static int addq(kqueue *q, int c);
98 static int removeq(kqueue *q);
99 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
100 static int wait_for_data(atkbdc_softc_t *kbdc);
101 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
102 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
103 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
104 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
107 *atkbdc_get_softc(int unit)
111 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
113 sc = atkbdc_softc[unit];
115 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
116 atkbdc_softc[unit] = sc;
122 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
124 if (rman_get_start(port0) <= 0)
126 if (rman_get_start(port1) <= 0)
132 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
133 struct resource *port1)
135 return atkbdc_setup(sc, rman_get_bustag(port0),
136 rman_get_bushandle(port0),
137 rman_get_bushandle(port1));
140 /* the backdoor to the keyboard controller! XXX */
142 atkbdc_configure(void)
145 bus_space_handle_t h0;
146 bus_space_handle_t h1;
151 resource_int_value("atkbdc", 0, "port", &port0);
152 port1 = IO_KBD + KBD_STATUS_PORT;
154 resource_int_value("atkbdc", 0, "port", &port0);
157 /* XXX: tag should be passed from the caller */
158 #if defined(__i386__)
159 tag = I386_BUS_SPACE_IO;
160 #elif defined(__alpha__)
161 tag = ALPHA_BUS_SPACE_IO;
165 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
166 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
168 h0 = (bus_space_handle_t)port0;
169 h1 = (bus_space_handle_t)port1;
171 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
175 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
176 bus_space_handle_t h1)
178 if (sc->ioh0 == 0) { /* XXX */
179 sc->command_byte = -1;
180 sc->command_mask = 0;
182 sc->kbd.head = sc->kbd.tail = 0;
183 sc->aux.head = sc->aux.tail = 0;
185 sc->kbd.call_count = 0;
186 sc->kbd.qcount = sc->kbd.max_qcount = 0;
187 sc->aux.call_count = 0;
188 sc->aux.qcount = sc->aux.max_qcount = 0;
197 /* open a keyboard controller */
199 atkbdc_open(int unit)
205 if ((atkbdc_softc[unit]->port0 != NULL)
206 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
207 return (KBDC)atkbdc_softc[unit];
212 * I/O access arbitration in `kbdio'
214 * The `kbdio' module uses a simplistic convention to arbitrate
215 * I/O access to the controller/keyboard/mouse. The convention requires
216 * close cooperation of the calling device driver.
218 * The device drivers which utilize the `kbdio' module are assumed to
219 * have the following set of routines.
220 * a. An interrupt handler (the bottom half of the driver).
221 * b. Timeout routines which may briefly poll the keyboard controller.
222 * c. Routines outside interrupt context (the top half of the driver).
223 * They should follow the rules below:
224 * 1. The interrupt handler may assume that it always has full access
225 * to the controller/keyboard/mouse.
226 * 2. The other routines must issue `spltty()' if they wish to
227 * prevent the interrupt handler from accessing
228 * the controller/keyboard/mouse.
229 * 3. The timeout routines and the top half routines of the device driver
230 * arbitrate I/O access by observing the lock flag in `kbdio'.
231 * The flag is manipulated via `kbdc_lock()'; when one wants to
232 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
233 * the call returns with TRUE. Otherwise the caller must back off.
234 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
235 * is finished. This mechanism does not prevent the interrupt
236 * handler from being invoked at any time and carrying out I/O.
237 * Therefore, `spltty()' must be strategically placed in the device
238 * driver code. Also note that the timeout routine may interrupt
239 * `kbdc_lock()' called by the top half of the driver, but this
240 * interruption is OK so long as the timeout routine observes
242 * 4. The interrupt and timeout routines should not extend I/O operation
243 * across more than one interrupt or timeout; they must complete any
244 * necessary I/O operation within one invocation of the routine.
245 * This means that if the timeout routine acquires the lock flag,
246 * it must reset the flag to FALSE before it returns.
249 /* set/reset polling lock */
251 kbdc_lock(KBDC p, int lock)
255 prevlock = kbdcp(p)->lock;
256 kbdcp(p)->lock = lock;
258 return (prevlock != lock);
261 /* check if any data is waiting to be processed */
263 kbdc_data_ready(KBDC p)
265 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
266 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
269 /* queuing functions */
272 addq(kqueue *q, int c)
274 if (nextq(q->tail) != q->head) {
276 q->tail = nextq(q->tail);
280 if (q->qcount > q->max_qcount)
281 q->max_qcount = q->qcount;
293 if (q->tail != q->head) {
295 q->head = nextq(q->head);
305 * device I/O routines
308 wait_while_controller_busy(struct atkbdc_softc *kbdc)
310 /* CPU will stay inside the loop for 100msec at most */
314 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
315 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
316 DELAY(KBDD_DELAYTIME);
317 addq(&kbdc->kbd, read_data(kbdc));
318 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
319 DELAY(KBDD_DELAYTIME);
320 addq(&kbdc->aux, read_data(kbdc));
322 DELAY(KBDC_DELAYTIME);
330 * wait for any data; whether it's from the controller,
331 * the keyboard, or the aux device.
334 wait_for_data(struct atkbdc_softc *kbdc)
336 /* CPU will stay inside the loop for 200msec at most */
340 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
341 DELAY(KBDC_DELAYTIME);
345 DELAY(KBDD_DELAYTIME);
349 /* wait for data from the keyboard */
351 wait_for_kbd_data(struct atkbdc_softc *kbdc)
353 /* CPU will stay inside the loop for 200msec at most */
357 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
358 != KBDS_KBD_BUFFER_FULL) {
359 if (f == KBDS_AUX_BUFFER_FULL) {
360 DELAY(KBDD_DELAYTIME);
361 addq(&kbdc->aux, read_data(kbdc));
363 DELAY(KBDC_DELAYTIME);
367 DELAY(KBDD_DELAYTIME);
372 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
373 * queue anything else.
376 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
378 /* CPU will stay inside the loop for 200msec at most */
383 while (retry-- > 0) {
384 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
385 DELAY(KBDD_DELAYTIME);
387 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
388 if ((b == KBD_ACK) || (b == KBD_RESEND)
389 || (b == KBD_RESET_FAIL))
392 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
396 DELAY(KBDC_DELAYTIME);
401 /* wait for data from the aux device */
403 wait_for_aux_data(struct atkbdc_softc *kbdc)
405 /* CPU will stay inside the loop for 200msec at most */
409 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
410 != KBDS_AUX_BUFFER_FULL) {
411 if (f == KBDS_KBD_BUFFER_FULL) {
412 DELAY(KBDD_DELAYTIME);
413 addq(&kbdc->kbd, read_data(kbdc));
415 DELAY(KBDC_DELAYTIME);
419 DELAY(KBDD_DELAYTIME);
424 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
425 * queue anything else.
428 wait_for_aux_ack(struct atkbdc_softc *kbdc)
430 /* CPU will stay inside the loop for 200msec at most */
435 while (retry-- > 0) {
436 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
437 DELAY(KBDD_DELAYTIME);
439 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
440 if ((b == PSM_ACK) || (b == PSM_RESEND)
441 || (b == PSM_RESET_FAIL))
444 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
448 DELAY(KBDC_DELAYTIME);
453 /* write a one byte command to the controller */
455 write_controller_command(KBDC p, int c)
457 if (!wait_while_controller_busy(kbdcp(p)))
459 write_command(kbdcp(p), c);
463 /* write a one byte data to the controller */
465 write_controller_data(KBDC p, int c)
467 if (!wait_while_controller_busy(kbdcp(p)))
469 write_data(kbdcp(p), c);
473 /* write a one byte keyboard command */
475 write_kbd_command(KBDC p, int c)
477 if (!wait_while_controller_busy(kbdcp(p)))
479 write_data(kbdcp(p), c);
483 /* write a one byte auxiliary device command */
485 write_aux_command(KBDC p, int c)
487 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
489 return write_controller_data(p, c);
492 /* send a command to the keyboard and wait for ACK */
494 send_kbd_command(KBDC p, int c)
496 int retry = KBD_MAXRETRY;
499 while (retry-- > 0) {
500 if (!write_kbd_command(p, c))
502 res = wait_for_kbd_ack(kbdcp(p));
509 /* send a command to the auxiliary device and wait for ACK */
511 send_aux_command(KBDC p, int c)
513 int retry = KBD_MAXRETRY;
516 while (retry-- > 0) {
517 if (!write_aux_command(p, c))
521 * The aux device may have already sent one or two bytes of
522 * status data, when a command is received. It will immediately
523 * stop data transmission, thus, leaving an incomplete data
524 * packet in our buffer. We have to discard any unprocessed
525 * data in order to remove such packets. Well, we may remove
526 * unprocessed, but necessary data byte as well...
528 emptyq(&kbdcp(p)->aux);
529 res = wait_for_aux_ack(kbdcp(p));
536 /* send a command and a data to the keyboard, wait for ACKs */
538 send_kbd_command_and_data(KBDC p, int c, int d)
543 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
544 if (!write_kbd_command(p, c))
546 res = wait_for_kbd_ack(kbdcp(p));
549 else if (res != KBD_RESEND)
555 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
556 if (!write_kbd_command(p, d))
558 res = wait_for_kbd_ack(kbdcp(p));
559 if (res != KBD_RESEND)
565 /* send a command and a data to the auxiliary device, wait for ACKs */
567 send_aux_command_and_data(KBDC p, int c, int d)
572 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
573 if (!write_aux_command(p, c))
575 emptyq(&kbdcp(p)->aux);
576 res = wait_for_aux_ack(kbdcp(p));
579 else if (res != PSM_RESEND)
585 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
586 if (!write_aux_command(p, d))
588 res = wait_for_aux_ack(kbdcp(p));
589 if (res != PSM_RESEND)
596 * read one byte from any source; whether from the controller,
597 * the keyboard, or the aux device
600 read_controller_data(KBDC p)
602 if (availq(&kbdcp(p)->kbd))
603 return removeq(&kbdcp(p)->kbd);
604 if (availq(&kbdcp(p)->aux))
605 return removeq(&kbdcp(p)->aux);
606 if (!wait_for_data(kbdcp(p)))
607 return -1; /* timeout */
608 return read_data(kbdcp(p));
615 /* read one byte from the keyboard */
617 read_kbd_data(KBDC p)
622 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
623 "aux q: %d calls, max %d chars\n",
624 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
625 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
629 if (availq(&kbdcp(p)->kbd))
630 return removeq(&kbdcp(p)->kbd);
631 if (!wait_for_kbd_data(kbdcp(p)))
632 return -1; /* timeout */
633 return read_data(kbdcp(p));
636 /* read one byte from the keyboard, but return immediately if
640 read_kbd_data_no_wait(KBDC p)
647 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
648 "aux q: %d calls, max %d chars\n",
649 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
650 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
654 if (availq(&kbdcp(p)->kbd))
655 return removeq(&kbdcp(p)->kbd);
656 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
657 if (f == KBDS_AUX_BUFFER_FULL) {
658 DELAY(KBDD_DELAYTIME);
659 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
660 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
662 if (f == KBDS_KBD_BUFFER_FULL) {
663 DELAY(KBDD_DELAYTIME);
664 return read_data(kbdcp(p));
666 return -1; /* no data */
669 /* read one byte from the aux device */
671 read_aux_data(KBDC p)
673 if (availq(&kbdcp(p)->aux))
674 return removeq(&kbdcp(p)->aux);
675 if (!wait_for_aux_data(kbdcp(p)))
676 return -1; /* timeout */
677 return read_data(kbdcp(p));
680 /* read one byte from the aux device, but return immediately if
684 read_aux_data_no_wait(KBDC p)
688 if (availq(&kbdcp(p)->aux))
689 return removeq(&kbdcp(p)->aux);
690 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
691 if (f == KBDS_KBD_BUFFER_FULL) {
692 DELAY(KBDD_DELAYTIME);
693 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
694 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
696 if (f == KBDS_AUX_BUFFER_FULL) {
697 DELAY(KBDD_DELAYTIME);
698 return read_data(kbdcp(p));
700 return -1; /* no data */
703 /* discard data from the keyboard */
705 empty_kbd_buffer(KBDC p, int wait)
716 for (t = wait; t > 0; ) {
717 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
718 DELAY(KBDD_DELAYTIME);
719 b = read_data(kbdcp(p));
720 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
721 addq(&kbdcp(p)->aux, b);
735 if ((c1 > 0) || (c2 > 0))
736 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
739 emptyq(&kbdcp(p)->kbd);
742 /* discard data from the aux device */
744 empty_aux_buffer(KBDC p, int wait)
755 for (t = wait; t > 0; ) {
756 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
757 DELAY(KBDD_DELAYTIME);
758 b = read_data(kbdcp(p));
759 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
760 addq(&kbdcp(p)->kbd, b);
774 if ((c1 > 0) || (c2 > 0))
775 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
778 emptyq(&kbdcp(p)->aux);
781 /* discard any data from the keyboard or the aux device */
783 empty_both_buffers(KBDC p, int wait)
793 for (t = wait; t > 0; ) {
794 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
795 DELAY(KBDD_DELAYTIME);
796 (void)read_data(kbdcp(p));
798 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
810 if ((c1 > 0) || (c2 > 0))
811 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
814 emptyq(&kbdcp(p)->kbd);
815 emptyq(&kbdcp(p)->aux);
818 /* keyboard and mouse device control */
820 /* NOTE: enable the keyboard port but disable the keyboard
821 * interrupt before calling "reset_kbd()".
826 int retry = KBD_MAXRETRY;
827 int again = KBD_MAXWAIT;
828 int c = KBD_RESEND; /* keep the compiler happy */
830 while (retry-- > 0) {
831 empty_both_buffers(p, 10);
832 if (!write_kbd_command(p, KBDC_RESET_KBD))
834 emptyq(&kbdcp(p)->kbd);
835 c = read_controller_data(p);
836 if (verbose || bootverbose)
837 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
838 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
844 while (again-- > 0) {
845 /* wait awhile, well, in fact we must wait quite loooooooooooong */
846 DELAY(KBD_RESETDELAY*1000);
847 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
848 if (c != -1) /* wait again if the controller is not ready */
851 if (verbose || bootverbose)
852 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
853 if (c != KBD_RESET_DONE)
858 /* NOTE: enable the aux port but disable the aux interrupt
859 * before calling `reset_aux_dev()'.
862 reset_aux_dev(KBDC p)
864 int retry = KBD_MAXRETRY;
865 int again = KBD_MAXWAIT;
866 int c = PSM_RESEND; /* keep the compiler happy */
868 while (retry-- > 0) {
869 empty_both_buffers(p, 10);
870 if (!write_aux_command(p, PSMC_RESET_DEV))
872 emptyq(&kbdcp(p)->aux);
873 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
874 for (again = KBD_MAXWAIT; again > 0; --again) {
875 DELAY(KBD_RESETDELAY*1000);
876 c = read_aux_data_no_wait(p);
880 if (verbose || bootverbose)
881 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
882 if (c == PSM_ACK) /* aux dev is about to reset... */
888 for (again = KBD_MAXWAIT; again > 0; --again) {
889 /* wait awhile, well, quite looooooooooooong */
890 DELAY(KBD_RESETDELAY*1000);
891 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
892 if (c != -1) /* wait again if the controller is not ready */
895 if (verbose || bootverbose)
896 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
897 if (c != PSM_RESET_DONE) /* reset status */
900 c = read_aux_data(p); /* device ID */
901 if (verbose || bootverbose)
902 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
903 /* NOTE: we could check the device ID now, but leave it later... */
907 /* controller diagnostics and setup */
910 test_controller(KBDC p)
912 int retry = KBD_MAXRETRY;
913 int again = KBD_MAXWAIT;
914 int c = KBD_DIAG_FAIL;
916 while (retry-- > 0) {
917 empty_both_buffers(p, 10);
918 if (write_controller_command(p, KBDC_DIAGNOSE))
924 emptyq(&kbdcp(p)->kbd);
925 while (again-- > 0) {
927 DELAY(KBD_RESETDELAY*1000);
928 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
929 if (c != -1) /* wait again if the controller is not ready */
932 if (verbose || bootverbose)
933 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
934 return (c == KBD_DIAG_DONE);
938 test_kbd_port(KBDC p)
940 int retry = KBD_MAXRETRY;
941 int again = KBD_MAXWAIT;
944 while (retry-- > 0) {
945 empty_both_buffers(p, 10);
946 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
952 emptyq(&kbdcp(p)->kbd);
953 while (again-- > 0) {
954 c = read_controller_data(p);
955 if (c != -1) /* try again if the controller is not ready */
958 if (verbose || bootverbose)
959 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
964 test_aux_port(KBDC p)
966 int retry = KBD_MAXRETRY;
967 int again = KBD_MAXWAIT;
970 while (retry-- > 0) {
971 empty_both_buffers(p, 10);
972 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
978 emptyq(&kbdcp(p)->kbd);
979 while (again-- > 0) {
980 c = read_controller_data(p);
981 if (c != -1) /* try again if the controller is not ready */
984 if (verbose || bootverbose)
985 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
990 kbdc_get_device_mask(KBDC p)
992 return kbdcp(p)->command_mask;
996 kbdc_set_device_mask(KBDC p, int mask)
998 kbdcp(p)->command_mask =
999 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1003 get_controller_command_byte(KBDC p)
1005 if (kbdcp(p)->command_byte != -1)
1006 return kbdcp(p)->command_byte;
1007 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1009 emptyq(&kbdcp(p)->kbd);
1010 kbdcp(p)->command_byte = read_controller_data(p);
1011 return kbdcp(p)->command_byte;
1015 set_controller_command_byte(KBDC p, int mask, int command)
1017 if (get_controller_command_byte(p) == -1)
1020 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1021 if (command & KBD_DISABLE_KBD_PORT) {
1022 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1025 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1027 if (!write_controller_data(p, command))
1029 kbdcp(p)->command_byte = command;
1032 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",