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.10 2007/04/22 10:43:00 y0netan1 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>
45 #include <machine/clock.h>
47 #include "atkbdcreg.h"
49 #include <bus/isa/isareg.h>
53 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
58 #define MAX(x, y) ((x) > (y) ? (x) : (y))
61 #define kbdcp(p) ((atkbdc_softc_t *)(p))
62 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
63 #define availq(q) ((q)->head != (q)->tail)
65 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
67 #define emptyq(q) ((q)->tail = (q)->head = 0)
70 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
71 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
72 #define write_data(k, d) \
73 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
74 #define write_command(k, d) \
75 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
80 * We always need at least one copy of the kbdc_softc struct for the
81 * low-level console. As the low-level console accesses the keyboard
82 * controller before kbdc, and all other devices, is probed, we
83 * statically allocate one entry. XXX
85 static atkbdc_softc_t default_kbdc;
86 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
88 static int verbose = KBDIO_DEBUG;
90 /* function prototypes */
92 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
93 bus_space_handle_t h0, bus_space_handle_t h1);
94 static int addq(kbdkqueue *q, int c);
95 static int removeq(kbdkqueue *q);
96 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
97 static int wait_for_data(atkbdc_softc_t *kbdc);
98 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
99 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
100 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
101 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
104 atkbdc_get_softc(int unit)
108 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
110 sc = atkbdc_softc[unit];
112 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
113 atkbdc_softc[unit] = sc;
119 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
121 if (rman_get_start(port0) <= 0)
123 if (rman_get_start(port1) <= 0)
129 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
130 struct resource *port1)
132 return atkbdc_setup(sc, rman_get_bustag(port0),
133 rman_get_bushandle(port0),
134 rman_get_bushandle(port1));
137 /* the backdoor to the keyboard controller! XXX */
139 atkbdc_configure(void)
142 bus_space_handle_t h0;
143 bus_space_handle_t h1;
146 #if defined(__i386__)
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;
163 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
164 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
166 h0 = (bus_space_handle_t)port0;
167 h1 = (bus_space_handle_t)port1;
170 #if defined(__i386__)
172 * Check if we really have AT keyboard controller. Poll status
173 * register until we get "all clear" indication. If no such
174 * indication comes, it probably means that there is no AT
175 * keyboard controller present. Give up in such case. Check relies
176 * on the fact that reading from non-existing in/out port returns
177 * 0xff on i386. May or may not be true on other platforms.
179 for (i = 65536; i != 0; --i) {
180 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
187 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
191 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
192 bus_space_handle_t h1)
194 if (sc->ioh0 == 0) { /* XXX */
195 sc->command_byte = -1;
196 sc->command_mask = 0;
198 sc->kbd.head = sc->kbd.tail = 0;
199 sc->aux.head = sc->aux.tail = 0;
201 sc->kbd.call_count = 0;
202 sc->kbd.qcount = sc->kbd.max_qcount = 0;
203 sc->aux.call_count = 0;
204 sc->aux.qcount = sc->aux.max_qcount = 0;
213 /* open a keyboard controller */
215 atkbdc_open(int unit)
221 if ((atkbdc_softc[unit]->port0 != NULL)
222 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
223 return (KBDC)atkbdc_softc[unit];
228 * I/O access arbitration in `kbdio'
230 * The `kbdio' module uses a simplistic convention to arbitrate
231 * I/O access to the controller/keyboard/mouse. The convention requires
232 * close cooperation of the calling device driver.
234 * The device drivers which utilize the `kbdio' module are assumed to
235 * have the following set of routines.
236 * a. An interrupt handler (the bottom half of the driver).
237 * b. Timeout routines which may briefly poll the keyboard controller.
238 * c. Routines outside interrupt context (the top half of the driver).
239 * They should follow the rules below:
240 * 1. The interrupt handler may assume that it always has full access
241 * to the controller/keyboard/mouse.
242 * 2. The other routines must issue `spltty()' if they wish to
243 * prevent the interrupt handler from accessing
244 * the controller/keyboard/mouse.
245 * 3. The timeout routines and the top half routines of the device driver
246 * arbitrate I/O access by observing the lock flag in `kbdio'.
247 * The flag is manipulated via `kbdc_lock()'; when one wants to
248 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
249 * the call returns with TRUE. Otherwise the caller must back off.
250 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
251 * is finished. This mechanism does not prevent the interrupt
252 * handler from being invoked at any time and carrying out I/O.
253 * Therefore, `spltty()' must be strategically placed in the device
254 * driver code. Also note that the timeout routine may interrupt
255 * `kbdc_lock()' called by the top half of the driver, but this
256 * interruption is OK so long as the timeout routine observes
258 * 4. The interrupt and timeout routines should not extend I/O operation
259 * across more than one interrupt or timeout; they must complete any
260 * necessary I/O operation within one invocation of the routine.
261 * This means that if the timeout routine acquires the lock flag,
262 * it must reset the flag to FALSE before it returns.
265 /* set/reset polling lock */
267 kbdc_lock(KBDC p, int lock)
271 prevlock = kbdcp(p)->lock;
272 kbdcp(p)->lock = lock;
274 return (prevlock != lock);
277 /* check if any data is waiting to be processed */
279 kbdc_data_ready(KBDC p)
281 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
282 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
285 /* queuing functions */
288 addq(kbdkqueue *q, int c)
290 if (nextq(q->tail) != q->head) {
292 q->tail = nextq(q->tail);
296 if (q->qcount > q->max_qcount)
297 q->max_qcount = q->qcount;
305 removeq(kbdkqueue *q)
309 if (q->tail != q->head) {
311 q->head = nextq(q->head);
321 * device I/O routines
324 wait_while_controller_busy(struct atkbdc_softc *kbdc)
326 /* CPU will stay inside the loop for 100msec at most */
330 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
331 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
332 DELAY(KBDD_DELAYTIME);
333 addq(&kbdc->kbd, read_data(kbdc));
334 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
335 DELAY(KBDD_DELAYTIME);
336 addq(&kbdc->aux, read_data(kbdc));
338 DELAY(KBDC_DELAYTIME);
346 * wait for any data; whether it's from the controller,
347 * the keyboard, or the aux device.
350 wait_for_data(struct atkbdc_softc *kbdc)
352 /* CPU will stay inside the loop for 200msec at most */
356 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
357 DELAY(KBDC_DELAYTIME);
361 DELAY(KBDD_DELAYTIME);
365 /* wait for data from the keyboard */
367 wait_for_kbd_data(struct atkbdc_softc *kbdc)
369 /* CPU will stay inside the loop for 200msec at most */
373 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
374 != KBDS_KBD_BUFFER_FULL) {
375 if (f == KBDS_AUX_BUFFER_FULL) {
376 DELAY(KBDD_DELAYTIME);
377 addq(&kbdc->aux, read_data(kbdc));
379 DELAY(KBDC_DELAYTIME);
383 DELAY(KBDD_DELAYTIME);
388 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
389 * queue anything else.
392 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
394 /* CPU will stay inside the loop for 200msec at most */
399 while (retry-- > 0) {
400 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
401 DELAY(KBDD_DELAYTIME);
403 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
404 if ((b == KBD_ACK) || (b == KBD_RESEND)
405 || (b == KBD_RESET_FAIL))
408 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
412 DELAY(KBDC_DELAYTIME);
417 /* wait for data from the aux device */
419 wait_for_aux_data(struct atkbdc_softc *kbdc)
421 /* CPU will stay inside the loop for 200msec at most */
425 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
426 != KBDS_AUX_BUFFER_FULL) {
427 if (f == KBDS_KBD_BUFFER_FULL) {
428 DELAY(KBDD_DELAYTIME);
429 addq(&kbdc->kbd, read_data(kbdc));
431 DELAY(KBDC_DELAYTIME);
435 DELAY(KBDD_DELAYTIME);
440 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
441 * queue anything else.
444 wait_for_aux_ack(struct atkbdc_softc *kbdc)
446 /* CPU will stay inside the loop for 200msec at most */
451 while (retry-- > 0) {
452 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
453 DELAY(KBDD_DELAYTIME);
455 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
456 if ((b == PSM_ACK) || (b == PSM_RESEND)
457 || (b == PSM_RESET_FAIL))
460 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
464 DELAY(KBDC_DELAYTIME);
469 /* write a one byte command to the controller */
471 write_controller_command(KBDC p, int c)
473 if (!wait_while_controller_busy(kbdcp(p)))
475 write_command(kbdcp(p), c);
479 /* write a one byte data to the controller */
481 write_controller_data(KBDC p, int c)
483 if (!wait_while_controller_busy(kbdcp(p)))
485 write_data(kbdcp(p), c);
489 /* write a one byte keyboard command */
491 write_kbd_command(KBDC p, int c)
493 if (!wait_while_controller_busy(kbdcp(p)))
495 write_data(kbdcp(p), c);
499 /* write a one byte auxiliary device command */
501 write_aux_command(KBDC p, int c)
503 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
505 return write_controller_data(p, c);
508 /* send a command to the keyboard and wait for ACK */
510 send_kbd_command(KBDC p, int c)
512 int retry = KBD_MAXRETRY;
515 while (retry-- > 0) {
516 if (!write_kbd_command(p, c))
518 res = wait_for_kbd_ack(kbdcp(p));
525 /* send a command to the auxiliary device and wait for ACK */
527 send_aux_command(KBDC p, int c)
529 int retry = KBD_MAXRETRY;
532 while (retry-- > 0) {
533 if (!write_aux_command(p, c))
537 * The aux device may have already sent one or two bytes of
538 * status data, when a command is received. It will immediately
539 * stop data transmission, thus, leaving an incomplete data
540 * packet in our buffer. We have to discard any unprocessed
541 * data in order to remove such packets. Well, we may remove
542 * unprocessed, but necessary data byte as well...
544 emptyq(&kbdcp(p)->aux);
545 res = wait_for_aux_ack(kbdcp(p));
552 /* send a command and a data to the keyboard, wait for ACKs */
554 send_kbd_command_and_data(KBDC p, int c, int d)
559 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
560 if (!write_kbd_command(p, c))
562 res = wait_for_kbd_ack(kbdcp(p));
565 else if (res != KBD_RESEND)
571 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
572 if (!write_kbd_command(p, d))
574 res = wait_for_kbd_ack(kbdcp(p));
575 if (res != KBD_RESEND)
581 /* send a command and a data to the auxiliary device, wait for ACKs */
583 send_aux_command_and_data(KBDC p, int c, int d)
588 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
589 if (!write_aux_command(p, c))
591 emptyq(&kbdcp(p)->aux);
592 res = wait_for_aux_ack(kbdcp(p));
595 else if (res != PSM_RESEND)
601 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
602 if (!write_aux_command(p, d))
604 res = wait_for_aux_ack(kbdcp(p));
605 if (res != PSM_RESEND)
612 * read one byte from any source; whether from the controller,
613 * the keyboard, or the aux device
616 read_controller_data(KBDC p)
618 if (availq(&kbdcp(p)->kbd))
619 return removeq(&kbdcp(p)->kbd);
620 if (availq(&kbdcp(p)->aux))
621 return removeq(&kbdcp(p)->aux);
622 if (!wait_for_data(kbdcp(p)))
623 return -1; /* timeout */
624 return read_data(kbdcp(p));
631 /* read one byte from the keyboard */
633 read_kbd_data(KBDC p)
638 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
639 "aux q: %d calls, max %d chars\n",
640 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
641 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
645 if (availq(&kbdcp(p)->kbd))
646 return removeq(&kbdcp(p)->kbd);
647 if (!wait_for_kbd_data(kbdcp(p)))
648 return -1; /* timeout */
649 return read_data(kbdcp(p));
652 /* read one byte from the keyboard, but return immediately if
656 read_kbd_data_no_wait(KBDC p)
663 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
664 "aux q: %d calls, max %d chars\n",
665 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
666 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
670 if (availq(&kbdcp(p)->kbd))
671 return removeq(&kbdcp(p)->kbd);
672 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
673 if (f == KBDS_AUX_BUFFER_FULL) {
674 DELAY(KBDD_DELAYTIME);
675 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
676 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
678 if (f == KBDS_KBD_BUFFER_FULL) {
679 DELAY(KBDD_DELAYTIME);
680 return read_data(kbdcp(p));
682 return -1; /* no data */
685 /* read one byte from the aux device */
687 read_aux_data(KBDC p)
689 if (availq(&kbdcp(p)->aux))
690 return removeq(&kbdcp(p)->aux);
691 if (!wait_for_aux_data(kbdcp(p)))
692 return -1; /* timeout */
693 return read_data(kbdcp(p));
696 /* read one byte from the aux device, but return immediately if
700 read_aux_data_no_wait(KBDC p)
704 if (availq(&kbdcp(p)->aux))
705 return removeq(&kbdcp(p)->aux);
706 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
707 if (f == KBDS_KBD_BUFFER_FULL) {
708 DELAY(KBDD_DELAYTIME);
709 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
710 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
712 if (f == KBDS_AUX_BUFFER_FULL) {
713 DELAY(KBDD_DELAYTIME);
714 return read_data(kbdcp(p));
716 return -1; /* no data */
719 /* discard data from the keyboard */
721 empty_kbd_buffer(KBDC p, int wait)
732 for (t = wait; t > 0; ) {
733 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
734 DELAY(KBDD_DELAYTIME);
735 b = read_data(kbdcp(p));
736 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
737 addq(&kbdcp(p)->aux, b);
751 if ((c1 > 0) || (c2 > 0))
752 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
755 emptyq(&kbdcp(p)->kbd);
758 /* discard data from the aux device */
760 empty_aux_buffer(KBDC p, int wait)
771 for (t = wait; t > 0; ) {
772 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
773 DELAY(KBDD_DELAYTIME);
774 b = read_data(kbdcp(p));
775 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
776 addq(&kbdcp(p)->kbd, b);
790 if ((c1 > 0) || (c2 > 0))
791 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
794 emptyq(&kbdcp(p)->aux);
797 /* discard any data from the keyboard or the aux device */
799 empty_both_buffers(KBDC p, int wait)
809 for (t = wait; t > 0; ) {
810 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
811 DELAY(KBDD_DELAYTIME);
812 (void)read_data(kbdcp(p));
814 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
826 if ((c1 > 0) || (c2 > 0))
827 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
830 emptyq(&kbdcp(p)->kbd);
831 emptyq(&kbdcp(p)->aux);
834 /* keyboard and mouse device control */
836 /* NOTE: enable the keyboard port but disable the keyboard
837 * interrupt before calling "reset_kbd()".
842 int retry = KBD_MAXRETRY;
843 int again = KBD_MAXWAIT;
844 int c = KBD_RESEND; /* keep the compiler happy */
846 while (retry-- > 0) {
847 empty_both_buffers(p, 10);
848 if (!write_kbd_command(p, KBDC_RESET_KBD))
850 emptyq(&kbdcp(p)->kbd);
851 c = read_controller_data(p);
852 if (verbose || bootverbose)
853 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
854 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
860 while (again-- > 0) {
861 /* wait awhile, well, in fact we must wait quite loooooooooooong */
862 DELAY(KBD_RESETDELAY*1000);
863 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
864 if (c != -1) /* wait again if the controller is not ready */
867 if (verbose || bootverbose)
868 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
869 if (c != KBD_RESET_DONE)
874 /* NOTE: enable the aux port but disable the aux interrupt
875 * before calling `reset_aux_dev()'.
878 reset_aux_dev(KBDC p)
880 int retry = KBD_MAXRETRY;
881 int again = KBD_MAXWAIT;
882 int c = PSM_RESEND; /* keep the compiler happy */
884 while (retry-- > 0) {
885 empty_both_buffers(p, 10);
886 if (!write_aux_command(p, PSMC_RESET_DEV))
888 emptyq(&kbdcp(p)->aux);
889 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
890 for (again = KBD_MAXWAIT; again > 0; --again) {
891 DELAY(KBD_RESETDELAY*1000);
892 c = read_aux_data_no_wait(p);
896 if (verbose || bootverbose)
897 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
898 if (c == PSM_ACK) /* aux dev is about to reset... */
904 for (again = KBD_MAXWAIT; again > 0; --again) {
905 /* wait awhile, well, quite looooooooooooong */
906 DELAY(KBD_RESETDELAY*1000);
907 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
908 if (c != -1) /* wait again if the controller is not ready */
911 if (verbose || bootverbose)
912 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
913 if (c != PSM_RESET_DONE) /* reset status */
916 c = read_aux_data(p); /* device ID */
917 if (verbose || bootverbose)
918 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
919 /* NOTE: we could check the device ID now, but leave it later... */
923 /* controller diagnostics and setup */
926 test_controller(KBDC p)
928 int retry = KBD_MAXRETRY;
929 int again = KBD_MAXWAIT;
930 int c = KBD_DIAG_FAIL;
932 while (retry-- > 0) {
933 empty_both_buffers(p, 10);
934 if (write_controller_command(p, KBDC_DIAGNOSE))
940 emptyq(&kbdcp(p)->kbd);
941 while (again-- > 0) {
943 DELAY(KBD_RESETDELAY*1000);
944 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
945 if (c != -1) /* wait again if the controller is not ready */
948 if (verbose || bootverbose)
949 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
950 return (c == KBD_DIAG_DONE);
954 test_kbd_port(KBDC p)
956 int retry = KBD_MAXRETRY;
957 int again = KBD_MAXWAIT;
960 while (retry-- > 0) {
961 empty_both_buffers(p, 10);
962 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
968 emptyq(&kbdcp(p)->kbd);
969 while (again-- > 0) {
970 c = read_controller_data(p);
971 if (c != -1) /* try again if the controller is not ready */
974 if (verbose || bootverbose)
975 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
980 test_aux_port(KBDC p)
982 int retry = KBD_MAXRETRY;
983 int again = KBD_MAXWAIT;
986 while (retry-- > 0) {
987 empty_both_buffers(p, 10);
988 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
994 emptyq(&kbdcp(p)->kbd);
995 while (again-- > 0) {
996 c = read_controller_data(p);
997 if (c != -1) /* try again if the controller is not ready */
1000 if (verbose || bootverbose)
1001 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1006 kbdc_get_device_mask(KBDC p)
1008 return kbdcp(p)->command_mask;
1012 kbdc_set_device_mask(KBDC p, int mask)
1014 kbdcp(p)->command_mask =
1015 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1019 get_controller_command_byte(KBDC p)
1021 if (kbdcp(p)->command_byte != -1)
1022 return kbdcp(p)->command_byte;
1023 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1025 emptyq(&kbdcp(p)->kbd);
1026 kbdcp(p)->command_byte = read_controller_data(p);
1027 return kbdcp(p)->command_byte;
1031 set_controller_command_byte(KBDC p, int mask, int command)
1033 if (get_controller_command_byte(p) == -1)
1036 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1037 if (command & KBD_DISABLE_KBD_PORT) {
1038 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1041 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1043 if (!write_controller_data(p, command))
1045 kbdcp(p)->command_byte = command;
1048 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",