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/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/syslog.h>
44 #include <machine/bus_pio.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
50 #include <dev/kbd/atkbdcreg.h>
52 #include <isa/isareg.h>
56 #define MAXKBDC 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 = atkbdc_softc[unit]
116 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT | M_ZERO);
124 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
126 if (rman_get_start(port0) <= 0)
128 if (rman_get_start(port1) <= 0)
134 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
135 struct resource *port1)
137 return atkbdc_setup(sc, rman_get_bustag(port0),
138 rman_get_bushandle(port0),
139 rman_get_bushandle(port1));
142 /* the backdoor to the keyboard controller! XXX */
144 atkbdc_configure(void)
147 bus_space_handle_t h0;
148 bus_space_handle_t h1;
153 resource_int_value("atkbdc", 0, "port", &port0);
154 port1 = IO_KBD + KBD_STATUS_PORT;
156 resource_int_value("atkbdc", 0, "port", &port0);
159 /* XXX: tag should be passed from the caller */
160 #if defined(__i386__)
161 tag = I386_BUS_SPACE_IO;
162 #elif defined(__amd64__)
163 tag = AMD64_BUS_SPACE_IO;
164 #elif defined(__alpha__)
165 tag = busspace_isa_io;
166 #elif defined(__ia64__)
167 tag = IA64_BUS_SPACE_IO;
173 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
174 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
176 h0 = (bus_space_handle_t)port0;
177 h1 = (bus_space_handle_t)port1;
179 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
183 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
184 bus_space_handle_t h1)
186 if (sc->ioh0 == 0) { /* XXX */
187 sc->command_byte = -1;
188 sc->command_mask = 0;
190 sc->kbd.head = sc->kbd.tail = 0;
191 sc->aux.head = sc->aux.tail = 0;
193 sc->kbd.call_count = 0;
194 sc->kbd.qcount = sc->kbd.max_qcount = 0;
195 sc->aux.call_count = 0;
196 sc->aux.qcount = sc->aux.max_qcount = 0;
205 /* open a keyboard controller */
207 atkbdc_open(int unit)
213 if ((atkbdc_softc[unit]->port0 != NULL)
214 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
215 return (KBDC)atkbdc_softc[unit];
220 * I/O access arbitration in `kbdio'
222 * The `kbdio' module uses a simplistic convention to arbitrate
223 * I/O access to the controller/keyboard/mouse. The convention requires
224 * close cooperation of the calling device driver.
226 * The device drivers which utilize the `kbdio' module are assumed to
227 * have the following set of routines.
228 * a. An interrupt handler (the bottom half of the driver).
229 * b. Timeout routines which may briefly poll the keyboard controller.
230 * c. Routines outside interrupt context (the top half of the driver).
231 * They should follow the rules below:
232 * 1. The interrupt handler may assume that it always has full access
233 * to the controller/keyboard/mouse.
234 * 2. The other routines must issue `spltty()' if they wish to
235 * prevent the interrupt handler from accessing
236 * the controller/keyboard/mouse.
237 * 3. The timeout routines and the top half routines of the device driver
238 * arbitrate I/O access by observing the lock flag in `kbdio'.
239 * The flag is manipulated via `kbdc_lock()'; when one wants to
240 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
241 * the call returns with TRUE. Otherwise the caller must back off.
242 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
243 * is finished. This mechanism does not prevent the interrupt
244 * handler from being invoked at any time and carrying out I/O.
245 * Therefore, `spltty()' must be strategically placed in the device
246 * driver code. Also note that the timeout routine may interrupt
247 * `kbdc_lock()' called by the top half of the driver, but this
248 * interruption is OK so long as the timeout routine observes
250 * 4. The interrupt and timeout routines should not extend I/O operation
251 * across more than one interrupt or timeout; they must complete any
252 * necessary I/O operation within one invocation of the routine.
253 * This means that if the timeout routine acquires the lock flag,
254 * it must reset the flag to FALSE before it returns.
257 /* set/reset polling lock */
259 kbdc_lock(KBDC p, int lock)
263 prevlock = kbdcp(p)->lock;
264 kbdcp(p)->lock = lock;
266 return (prevlock != lock);
269 /* check if any data is waiting to be processed */
271 kbdc_data_ready(KBDC p)
273 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
274 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
277 /* queuing functions */
280 addq(kqueue *q, int c)
282 if (nextq(q->tail) != q->head) {
284 q->tail = nextq(q->tail);
288 if (q->qcount > q->max_qcount)
289 q->max_qcount = q->qcount;
301 if (q->tail != q->head) {
303 q->head = nextq(q->head);
313 * device I/O routines
316 wait_while_controller_busy(struct atkbdc_softc *kbdc)
318 /* CPU will stay inside the loop for 100msec at most */
322 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
323 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
324 DELAY(KBDD_DELAYTIME);
325 addq(&kbdc->kbd, read_data(kbdc));
326 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
327 DELAY(KBDD_DELAYTIME);
328 addq(&kbdc->aux, read_data(kbdc));
330 DELAY(KBDC_DELAYTIME);
338 * wait for any data; whether it's from the controller,
339 * the keyboard, or the aux device.
342 wait_for_data(struct atkbdc_softc *kbdc)
344 /* CPU will stay inside the loop for 200msec at most */
348 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
349 DELAY(KBDC_DELAYTIME);
353 DELAY(KBDD_DELAYTIME);
357 /* wait for data from the keyboard */
359 wait_for_kbd_data(struct atkbdc_softc *kbdc)
361 /* CPU will stay inside the loop for 200msec at most */
365 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
366 != KBDS_KBD_BUFFER_FULL) {
367 if (f == KBDS_AUX_BUFFER_FULL) {
368 DELAY(KBDD_DELAYTIME);
369 addq(&kbdc->aux, read_data(kbdc));
371 DELAY(KBDC_DELAYTIME);
375 DELAY(KBDD_DELAYTIME);
380 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
381 * queue anything else.
384 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
386 /* CPU will stay inside the loop for 200msec at most */
391 while (retry-- > 0) {
392 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
393 DELAY(KBDD_DELAYTIME);
395 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
396 if ((b == KBD_ACK) || (b == KBD_RESEND)
397 || (b == KBD_RESET_FAIL))
400 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
404 DELAY(KBDC_DELAYTIME);
409 /* wait for data from the aux device */
411 wait_for_aux_data(struct atkbdc_softc *kbdc)
413 /* CPU will stay inside the loop for 200msec at most */
417 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
418 != KBDS_AUX_BUFFER_FULL) {
419 if (f == KBDS_KBD_BUFFER_FULL) {
420 DELAY(KBDD_DELAYTIME);
421 addq(&kbdc->kbd, read_data(kbdc));
423 DELAY(KBDC_DELAYTIME);
427 DELAY(KBDD_DELAYTIME);
432 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
433 * queue anything else.
436 wait_for_aux_ack(struct atkbdc_softc *kbdc)
438 /* CPU will stay inside the loop for 200msec at most */
443 while (retry-- > 0) {
444 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
445 DELAY(KBDD_DELAYTIME);
447 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
448 if ((b == PSM_ACK) || (b == PSM_RESEND)
449 || (b == PSM_RESET_FAIL))
452 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
456 DELAY(KBDC_DELAYTIME);
461 /* write a one byte command to the controller */
463 write_controller_command(KBDC p, int c)
465 if (!wait_while_controller_busy(kbdcp(p)))
467 write_command(kbdcp(p), c);
471 /* write a one byte data to the controller */
473 write_controller_data(KBDC p, int c)
475 if (!wait_while_controller_busy(kbdcp(p)))
477 write_data(kbdcp(p), c);
481 /* write a one byte keyboard command */
483 write_kbd_command(KBDC p, int c)
485 if (!wait_while_controller_busy(kbdcp(p)))
487 write_data(kbdcp(p), c);
491 /* write a one byte auxiliary device command */
493 write_aux_command(KBDC p, int c)
495 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
497 return write_controller_data(p, c);
500 /* send a command to the keyboard and wait for ACK */
502 send_kbd_command(KBDC p, int c)
504 int retry = KBD_MAXRETRY;
507 while (retry-- > 0) {
508 if (!write_kbd_command(p, c))
510 res = wait_for_kbd_ack(kbdcp(p));
517 /* send a command to the auxiliary device and wait for ACK */
519 send_aux_command(KBDC p, int c)
521 int retry = KBD_MAXRETRY;
524 while (retry-- > 0) {
525 if (!write_aux_command(p, c))
529 * The aux device may have already sent one or two bytes of
530 * status data, when a command is received. It will immediately
531 * stop data transmission, thus, leaving an incomplete data
532 * packet in our buffer. We have to discard any unprocessed
533 * data in order to remove such packets. Well, we may remove
534 * unprocessed, but necessary data byte as well...
536 emptyq(&kbdcp(p)->aux);
537 res = wait_for_aux_ack(kbdcp(p));
544 /* send a command and a data to the keyboard, wait for ACKs */
546 send_kbd_command_and_data(KBDC p, int c, int d)
551 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
552 if (!write_kbd_command(p, c))
554 res = wait_for_kbd_ack(kbdcp(p));
557 else if (res != KBD_RESEND)
563 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
564 if (!write_kbd_command(p, d))
566 res = wait_for_kbd_ack(kbdcp(p));
567 if (res != KBD_RESEND)
573 /* send a command and a data to the auxiliary device, wait for ACKs */
575 send_aux_command_and_data(KBDC p, int c, int d)
580 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
581 if (!write_aux_command(p, c))
583 emptyq(&kbdcp(p)->aux);
584 res = wait_for_aux_ack(kbdcp(p));
587 else if (res != PSM_RESEND)
593 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
594 if (!write_aux_command(p, d))
596 res = wait_for_aux_ack(kbdcp(p));
597 if (res != PSM_RESEND)
604 * read one byte from any source; whether from the controller,
605 * the keyboard, or the aux device
608 read_controller_data(KBDC p)
610 if (availq(&kbdcp(p)->kbd))
611 return removeq(&kbdcp(p)->kbd);
612 if (availq(&kbdcp(p)->aux))
613 return removeq(&kbdcp(p)->aux);
614 if (!wait_for_data(kbdcp(p)))
615 return -1; /* timeout */
616 return read_data(kbdcp(p));
623 /* read one byte from the keyboard */
625 read_kbd_data(KBDC p)
630 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
631 "aux q: %d calls, max %d chars\n",
632 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
633 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
637 if (availq(&kbdcp(p)->kbd))
638 return removeq(&kbdcp(p)->kbd);
639 if (!wait_for_kbd_data(kbdcp(p)))
640 return -1; /* timeout */
641 return read_data(kbdcp(p));
644 /* read one byte from the keyboard, but return immediately if
648 read_kbd_data_no_wait(KBDC p)
655 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
656 "aux q: %d calls, max %d chars\n",
657 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
658 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
662 if (availq(&kbdcp(p)->kbd))
663 return removeq(&kbdcp(p)->kbd);
664 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
665 if (f == KBDS_AUX_BUFFER_FULL) {
666 DELAY(KBDD_DELAYTIME);
667 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
668 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
670 if (f == KBDS_KBD_BUFFER_FULL) {
671 DELAY(KBDD_DELAYTIME);
672 return read_data(kbdcp(p));
674 return -1; /* no data */
677 /* read one byte from the aux device */
679 read_aux_data(KBDC p)
681 if (availq(&kbdcp(p)->aux))
682 return removeq(&kbdcp(p)->aux);
683 if (!wait_for_aux_data(kbdcp(p)))
684 return -1; /* timeout */
685 return read_data(kbdcp(p));
688 /* read one byte from the aux device, but return immediately if
692 read_aux_data_no_wait(KBDC p)
696 if (availq(&kbdcp(p)->aux))
697 return removeq(&kbdcp(p)->aux);
698 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
699 if (f == KBDS_KBD_BUFFER_FULL) {
700 DELAY(KBDD_DELAYTIME);
701 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
702 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
704 if (f == KBDS_AUX_BUFFER_FULL) {
705 DELAY(KBDD_DELAYTIME);
706 return read_data(kbdcp(p));
708 return -1; /* no data */
711 /* discard data from the keyboard */
713 empty_kbd_buffer(KBDC p, int wait)
724 for (t = wait; t > 0; ) {
725 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
726 DELAY(KBDD_DELAYTIME);
727 b = read_data(kbdcp(p));
728 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
729 addq(&kbdcp(p)->aux, b);
743 if ((c1 > 0) || (c2 > 0))
744 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
747 emptyq(&kbdcp(p)->kbd);
750 /* discard data from the aux device */
752 empty_aux_buffer(KBDC p, int wait)
763 for (t = wait; t > 0; ) {
764 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
765 DELAY(KBDD_DELAYTIME);
766 b = read_data(kbdcp(p));
767 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
768 addq(&kbdcp(p)->kbd, b);
782 if ((c1 > 0) || (c2 > 0))
783 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
786 emptyq(&kbdcp(p)->aux);
789 /* discard any data from the keyboard or the aux device */
791 empty_both_buffers(KBDC p, int wait)
801 for (t = wait; t > 0; ) {
802 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
803 DELAY(KBDD_DELAYTIME);
804 (void)read_data(kbdcp(p));
806 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
818 if ((c1 > 0) || (c2 > 0))
819 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
822 emptyq(&kbdcp(p)->kbd);
823 emptyq(&kbdcp(p)->aux);
826 /* keyboard and mouse device control */
828 /* NOTE: enable the keyboard port but disable the keyboard
829 * interrupt before calling "reset_kbd()".
834 int retry = KBD_MAXRETRY;
835 int again = KBD_MAXWAIT;
836 int c = KBD_RESEND; /* keep the compiler happy */
838 while (retry-- > 0) {
839 empty_both_buffers(p, 10);
840 if (!write_kbd_command(p, KBDC_RESET_KBD))
842 emptyq(&kbdcp(p)->kbd);
843 c = read_controller_data(p);
844 if (verbose || bootverbose)
845 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
846 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
852 while (again-- > 0) {
853 /* wait awhile, well, in fact we must wait quite loooooooooooong */
854 DELAY(KBD_RESETDELAY*1000);
855 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
856 if (c != -1) /* wait again if the controller is not ready */
859 if (verbose || bootverbose)
860 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
861 if (c != KBD_RESET_DONE)
866 /* NOTE: enable the aux port but disable the aux interrupt
867 * before calling `reset_aux_dev()'.
870 reset_aux_dev(KBDC p)
872 int retry = KBD_MAXRETRY;
873 int again = KBD_MAXWAIT;
874 int c = PSM_RESEND; /* keep the compiler happy */
876 while (retry-- > 0) {
877 empty_both_buffers(p, 10);
878 if (!write_aux_command(p, PSMC_RESET_DEV))
880 emptyq(&kbdcp(p)->aux);
881 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
882 for (again = KBD_MAXWAIT; again > 0; --again) {
883 DELAY(KBD_RESETDELAY*1000);
884 c = read_aux_data_no_wait(p);
888 if (verbose || bootverbose)
889 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
890 if (c == PSM_ACK) /* aux dev is about to reset... */
896 for (again = KBD_MAXWAIT; again > 0; --again) {
897 /* wait awhile, well, quite looooooooooooong */
898 DELAY(KBD_RESETDELAY*1000);
899 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
900 if (c != -1) /* wait again if the controller is not ready */
903 if (verbose || bootverbose)
904 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
905 if (c != PSM_RESET_DONE) /* reset status */
908 c = read_aux_data(p); /* device ID */
909 if (verbose || bootverbose)
910 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
911 /* NOTE: we could check the device ID now, but leave it later... */
915 /* controller diagnostics and setup */
918 test_controller(KBDC p)
920 int retry = KBD_MAXRETRY;
921 int again = KBD_MAXWAIT;
922 int c = KBD_DIAG_FAIL;
924 while (retry-- > 0) {
925 empty_both_buffers(p, 10);
926 if (write_controller_command(p, KBDC_DIAGNOSE))
932 emptyq(&kbdcp(p)->kbd);
933 while (again-- > 0) {
935 DELAY(KBD_RESETDELAY*1000);
936 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
937 if (c != -1) /* wait again if the controller is not ready */
940 if (verbose || bootverbose)
941 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
942 return (c == KBD_DIAG_DONE);
947 test_kbd_port(KBDC p)
949 int retry = KBD_MAXRETRY;
950 int again = KBD_MAXWAIT;
952 int broken_kit_cmd = 0;
955 * Provide a way to disable using Keyboard Interface Test command, which
956 * may cause problems with some non-compliant hardware, resulting in
957 * machine being powered down early in the boot process.
959 * Particularly it's known that HP ZV5000 and Compaq R3000Z notebooks are
962 TUNABLE_INT_FETCH("hw.atkbdc.broken_kit_cmd", &broken_kit_cmd);
963 if (broken_kit_cmd != 0)
966 while (retry-- > 0) {
967 empty_both_buffers(p, 10);
968 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
974 emptyq(&kbdcp(p)->kbd);
975 while (again-- > 0) {
976 c = read_controller_data(p);
977 if (c != -1) /* try again if the controller is not ready */
980 if (verbose || bootverbose)
981 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
986 test_aux_port(KBDC p)
988 int retry = KBD_MAXRETRY;
989 int again = KBD_MAXWAIT;
992 while (retry-- > 0) {
993 empty_both_buffers(p, 10);
994 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1000 emptyq(&kbdcp(p)->kbd);
1001 while (again-- > 0) {
1002 c = read_controller_data(p);
1003 if (c != -1) /* try again if the controller is not ready */
1006 if (verbose || bootverbose)
1007 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1012 kbdc_get_device_mask(KBDC p)
1014 return kbdcp(p)->command_mask;
1018 kbdc_set_device_mask(KBDC p, int mask)
1020 kbdcp(p)->command_mask =
1021 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1025 get_controller_command_byte(KBDC p)
1027 if (kbdcp(p)->command_byte != -1)
1028 return kbdcp(p)->command_byte;
1029 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1031 emptyq(&kbdcp(p)->kbd);
1032 kbdcp(p)->command_byte = read_controller_data(p);
1033 return kbdcp(p)->command_byte;
1037 set_controller_command_byte(KBDC p, int mask, int command)
1039 if (get_controller_command_byte(p) == -1)
1042 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1043 if (command & KBD_DISABLE_KBD_PORT) {
1044 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1047 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1049 if (!write_controller_data(p, command))
1051 kbdcp(p)->command_byte = command;
1054 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",