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.2 2003/06/17 04:28:28 dillon Exp $
32 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
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 <dev/kbd/atkbdcreg.h>
52 #include <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 = atkbdc_softc[unit]
116 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
119 bzero(sc, sizeof(*sc));
125 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
127 if (rman_get_start(port0) <= 0)
129 if (rman_get_start(port1) <= 0)
135 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
136 struct resource *port1)
138 return atkbdc_setup(sc, rman_get_bustag(port0),
139 rman_get_bushandle(port0),
140 rman_get_bushandle(port1));
143 /* the backdoor to the keyboard controller! XXX */
145 atkbdc_configure(void)
148 bus_space_handle_t h0;
149 bus_space_handle_t h1;
154 resource_int_value("atkbdc", 0, "port", &port0);
155 port1 = IO_KBD + KBD_STATUS_PORT;
157 resource_int_value("atkbdc", 0, "port", &port0);
160 /* XXX: tag should be passed from the caller */
161 #if defined(__i386__)
162 tag = I386_BUS_SPACE_IO;
163 #elif defined(__alpha__)
164 tag = ALPHA_BUS_SPACE_IO;
168 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
169 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
171 h0 = (bus_space_handle_t)port0;
172 h1 = (bus_space_handle_t)port1;
174 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
178 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
179 bus_space_handle_t h1)
181 if (sc->ioh0 == 0) { /* XXX */
182 sc->command_byte = -1;
183 sc->command_mask = 0;
185 sc->kbd.head = sc->kbd.tail = 0;
186 sc->aux.head = sc->aux.tail = 0;
188 sc->kbd.call_count = 0;
189 sc->kbd.qcount = sc->kbd.max_qcount = 0;
190 sc->aux.call_count = 0;
191 sc->aux.qcount = sc->aux.max_qcount = 0;
200 /* open a keyboard controller */
202 atkbdc_open(int unit)
208 if ((atkbdc_softc[unit]->port0 != NULL)
209 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
210 return (KBDC)atkbdc_softc[unit];
215 * I/O access arbitration in `kbdio'
217 * The `kbdio' module uses a simplistic convention to arbitrate
218 * I/O access to the controller/keyboard/mouse. The convention requires
219 * close cooperation of the calling device driver.
221 * The device drivers which utilize the `kbdio' module are assumed to
222 * have the following set of routines.
223 * a. An interrupt handler (the bottom half of the driver).
224 * b. Timeout routines which may briefly poll the keyboard controller.
225 * c. Routines outside interrupt context (the top half of the driver).
226 * They should follow the rules below:
227 * 1. The interrupt handler may assume that it always has full access
228 * to the controller/keyboard/mouse.
229 * 2. The other routines must issue `spltty()' if they wish to
230 * prevent the interrupt handler from accessing
231 * the controller/keyboard/mouse.
232 * 3. The timeout routines and the top half routines of the device driver
233 * arbitrate I/O access by observing the lock flag in `kbdio'.
234 * The flag is manipulated via `kbdc_lock()'; when one wants to
235 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
236 * the call returns with TRUE. Otherwise the caller must back off.
237 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
238 * is finished. This mechanism does not prevent the interrupt
239 * handler from being invoked at any time and carrying out I/O.
240 * Therefore, `spltty()' must be strategically placed in the device
241 * driver code. Also note that the timeout routine may interrupt
242 * `kbdc_lock()' called by the top half of the driver, but this
243 * interruption is OK so long as the timeout routine observes
245 * 4. The interrupt and timeout routines should not extend I/O operation
246 * across more than one interrupt or timeout; they must complete any
247 * necessary I/O operation within one invocation of the routine.
248 * This means that if the timeout routine acquires the lock flag,
249 * it must reset the flag to FALSE before it returns.
252 /* set/reset polling lock */
254 kbdc_lock(KBDC p, int lock)
258 prevlock = kbdcp(p)->lock;
259 kbdcp(p)->lock = lock;
261 return (prevlock != lock);
264 /* check if any data is waiting to be processed */
266 kbdc_data_ready(KBDC p)
268 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
269 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
272 /* queuing functions */
275 addq(kqueue *q, int c)
277 if (nextq(q->tail) != q->head) {
279 q->tail = nextq(q->tail);
283 if (q->qcount > q->max_qcount)
284 q->max_qcount = q->qcount;
296 if (q->tail != q->head) {
298 q->head = nextq(q->head);
308 * device I/O routines
311 wait_while_controller_busy(struct atkbdc_softc *kbdc)
313 /* CPU will stay inside the loop for 100msec at most */
317 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
318 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
319 DELAY(KBDD_DELAYTIME);
320 addq(&kbdc->kbd, read_data(kbdc));
321 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
322 DELAY(KBDD_DELAYTIME);
323 addq(&kbdc->aux, read_data(kbdc));
325 DELAY(KBDC_DELAYTIME);
333 * wait for any data; whether it's from the controller,
334 * the keyboard, or the aux device.
337 wait_for_data(struct atkbdc_softc *kbdc)
339 /* CPU will stay inside the loop for 200msec at most */
343 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
344 DELAY(KBDC_DELAYTIME);
348 DELAY(KBDD_DELAYTIME);
352 /* wait for data from the keyboard */
354 wait_for_kbd_data(struct atkbdc_softc *kbdc)
356 /* CPU will stay inside the loop for 200msec at most */
360 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
361 != KBDS_KBD_BUFFER_FULL) {
362 if (f == KBDS_AUX_BUFFER_FULL) {
363 DELAY(KBDD_DELAYTIME);
364 addq(&kbdc->aux, read_data(kbdc));
366 DELAY(KBDC_DELAYTIME);
370 DELAY(KBDD_DELAYTIME);
375 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
376 * queue anything else.
379 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
381 /* CPU will stay inside the loop for 200msec at most */
386 while (retry-- > 0) {
387 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
388 DELAY(KBDD_DELAYTIME);
390 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
391 if ((b == KBD_ACK) || (b == KBD_RESEND)
392 || (b == KBD_RESET_FAIL))
395 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
399 DELAY(KBDC_DELAYTIME);
404 /* wait for data from the aux device */
406 wait_for_aux_data(struct atkbdc_softc *kbdc)
408 /* CPU will stay inside the loop for 200msec at most */
412 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
413 != KBDS_AUX_BUFFER_FULL) {
414 if (f == KBDS_KBD_BUFFER_FULL) {
415 DELAY(KBDD_DELAYTIME);
416 addq(&kbdc->kbd, read_data(kbdc));
418 DELAY(KBDC_DELAYTIME);
422 DELAY(KBDD_DELAYTIME);
427 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
428 * queue anything else.
431 wait_for_aux_ack(struct atkbdc_softc *kbdc)
433 /* CPU will stay inside the loop for 200msec at most */
438 while (retry-- > 0) {
439 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
440 DELAY(KBDD_DELAYTIME);
442 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
443 if ((b == PSM_ACK) || (b == PSM_RESEND)
444 || (b == PSM_RESET_FAIL))
447 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
451 DELAY(KBDC_DELAYTIME);
456 /* write a one byte command to the controller */
458 write_controller_command(KBDC p, int c)
460 if (!wait_while_controller_busy(kbdcp(p)))
462 write_command(kbdcp(p), c);
466 /* write a one byte data to the controller */
468 write_controller_data(KBDC p, int c)
470 if (!wait_while_controller_busy(kbdcp(p)))
472 write_data(kbdcp(p), c);
476 /* write a one byte keyboard command */
478 write_kbd_command(KBDC p, int c)
480 if (!wait_while_controller_busy(kbdcp(p)))
482 write_data(kbdcp(p), c);
486 /* write a one byte auxiliary device command */
488 write_aux_command(KBDC p, int c)
490 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
492 return write_controller_data(p, c);
495 /* send a command to the keyboard and wait for ACK */
497 send_kbd_command(KBDC p, int c)
499 int retry = KBD_MAXRETRY;
502 while (retry-- > 0) {
503 if (!write_kbd_command(p, c))
505 res = wait_for_kbd_ack(kbdcp(p));
512 /* send a command to the auxiliary device and wait for ACK */
514 send_aux_command(KBDC p, int c)
516 int retry = KBD_MAXRETRY;
519 while (retry-- > 0) {
520 if (!write_aux_command(p, c))
524 * The aux device may have already sent one or two bytes of
525 * status data, when a command is received. It will immediately
526 * stop data transmission, thus, leaving an incomplete data
527 * packet in our buffer. We have to discard any unprocessed
528 * data in order to remove such packets. Well, we may remove
529 * unprocessed, but necessary data byte as well...
531 emptyq(&kbdcp(p)->aux);
532 res = wait_for_aux_ack(kbdcp(p));
539 /* send a command and a data to the keyboard, wait for ACKs */
541 send_kbd_command_and_data(KBDC p, int c, int d)
546 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
547 if (!write_kbd_command(p, c))
549 res = wait_for_kbd_ack(kbdcp(p));
552 else if (res != KBD_RESEND)
558 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
559 if (!write_kbd_command(p, d))
561 res = wait_for_kbd_ack(kbdcp(p));
562 if (res != KBD_RESEND)
568 /* send a command and a data to the auxiliary device, wait for ACKs */
570 send_aux_command_and_data(KBDC p, int c, int d)
575 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
576 if (!write_aux_command(p, c))
578 emptyq(&kbdcp(p)->aux);
579 res = wait_for_aux_ack(kbdcp(p));
582 else if (res != PSM_RESEND)
588 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
589 if (!write_aux_command(p, d))
591 res = wait_for_aux_ack(kbdcp(p));
592 if (res != PSM_RESEND)
599 * read one byte from any source; whether from the controller,
600 * the keyboard, or the aux device
603 read_controller_data(KBDC p)
605 if (availq(&kbdcp(p)->kbd))
606 return removeq(&kbdcp(p)->kbd);
607 if (availq(&kbdcp(p)->aux))
608 return removeq(&kbdcp(p)->aux);
609 if (!wait_for_data(kbdcp(p)))
610 return -1; /* timeout */
611 return read_data(kbdcp(p));
618 /* read one byte from the keyboard */
620 read_kbd_data(KBDC p)
625 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
626 "aux q: %d calls, max %d chars\n",
627 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
628 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
632 if (availq(&kbdcp(p)->kbd))
633 return removeq(&kbdcp(p)->kbd);
634 if (!wait_for_kbd_data(kbdcp(p)))
635 return -1; /* timeout */
636 return read_data(kbdcp(p));
639 /* read one byte from the keyboard, but return immediately if
643 read_kbd_data_no_wait(KBDC p)
650 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
651 "aux q: %d calls, max %d chars\n",
652 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
653 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
657 if (availq(&kbdcp(p)->kbd))
658 return removeq(&kbdcp(p)->kbd);
659 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
660 if (f == KBDS_AUX_BUFFER_FULL) {
661 DELAY(KBDD_DELAYTIME);
662 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
663 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
665 if (f == KBDS_KBD_BUFFER_FULL) {
666 DELAY(KBDD_DELAYTIME);
667 return read_data(kbdcp(p));
669 return -1; /* no data */
672 /* read one byte from the aux device */
674 read_aux_data(KBDC p)
676 if (availq(&kbdcp(p)->aux))
677 return removeq(&kbdcp(p)->aux);
678 if (!wait_for_aux_data(kbdcp(p)))
679 return -1; /* timeout */
680 return read_data(kbdcp(p));
683 /* read one byte from the aux device, but return immediately if
687 read_aux_data_no_wait(KBDC p)
691 if (availq(&kbdcp(p)->aux))
692 return removeq(&kbdcp(p)->aux);
693 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
694 if (f == KBDS_KBD_BUFFER_FULL) {
695 DELAY(KBDD_DELAYTIME);
696 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
697 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
699 if (f == KBDS_AUX_BUFFER_FULL) {
700 DELAY(KBDD_DELAYTIME);
701 return read_data(kbdcp(p));
703 return -1; /* no data */
706 /* discard data from the keyboard */
708 empty_kbd_buffer(KBDC p, int wait)
719 for (t = wait; t > 0; ) {
720 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
721 DELAY(KBDD_DELAYTIME);
722 b = read_data(kbdcp(p));
723 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
724 addq(&kbdcp(p)->aux, b);
738 if ((c1 > 0) || (c2 > 0))
739 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
742 emptyq(&kbdcp(p)->kbd);
745 /* discard data from the aux device */
747 empty_aux_buffer(KBDC p, int wait)
758 for (t = wait; t > 0; ) {
759 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
760 DELAY(KBDD_DELAYTIME);
761 b = read_data(kbdcp(p));
762 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
763 addq(&kbdcp(p)->kbd, b);
777 if ((c1 > 0) || (c2 > 0))
778 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
781 emptyq(&kbdcp(p)->aux);
784 /* discard any data from the keyboard or the aux device */
786 empty_both_buffers(KBDC p, int wait)
796 for (t = wait; t > 0; ) {
797 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
798 DELAY(KBDD_DELAYTIME);
799 (void)read_data(kbdcp(p));
801 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
813 if ((c1 > 0) || (c2 > 0))
814 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
817 emptyq(&kbdcp(p)->kbd);
818 emptyq(&kbdcp(p)->aux);
821 /* keyboard and mouse device control */
823 /* NOTE: enable the keyboard port but disable the keyboard
824 * interrupt before calling "reset_kbd()".
829 int retry = KBD_MAXRETRY;
830 int again = KBD_MAXWAIT;
831 int c = KBD_RESEND; /* keep the compiler happy */
833 while (retry-- > 0) {
834 empty_both_buffers(p, 10);
835 if (!write_kbd_command(p, KBDC_RESET_KBD))
837 emptyq(&kbdcp(p)->kbd);
838 c = read_controller_data(p);
839 if (verbose || bootverbose)
840 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
841 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
847 while (again-- > 0) {
848 /* wait awhile, well, in fact we must wait quite loooooooooooong */
849 DELAY(KBD_RESETDELAY*1000);
850 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
851 if (c != -1) /* wait again if the controller is not ready */
854 if (verbose || bootverbose)
855 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
856 if (c != KBD_RESET_DONE)
861 /* NOTE: enable the aux port but disable the aux interrupt
862 * before calling `reset_aux_dev()'.
865 reset_aux_dev(KBDC p)
867 int retry = KBD_MAXRETRY;
868 int again = KBD_MAXWAIT;
869 int c = PSM_RESEND; /* keep the compiler happy */
871 while (retry-- > 0) {
872 empty_both_buffers(p, 10);
873 if (!write_aux_command(p, PSMC_RESET_DEV))
875 emptyq(&kbdcp(p)->aux);
876 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
877 for (again = KBD_MAXWAIT; again > 0; --again) {
878 DELAY(KBD_RESETDELAY*1000);
879 c = read_aux_data_no_wait(p);
883 if (verbose || bootverbose)
884 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
885 if (c == PSM_ACK) /* aux dev is about to reset... */
891 for (again = KBD_MAXWAIT; again > 0; --again) {
892 /* wait awhile, well, quite looooooooooooong */
893 DELAY(KBD_RESETDELAY*1000);
894 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
895 if (c != -1) /* wait again if the controller is not ready */
898 if (verbose || bootverbose)
899 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
900 if (c != PSM_RESET_DONE) /* reset status */
903 c = read_aux_data(p); /* device ID */
904 if (verbose || bootverbose)
905 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
906 /* NOTE: we could check the device ID now, but leave it later... */
910 /* controller diagnostics and setup */
913 test_controller(KBDC p)
915 int retry = KBD_MAXRETRY;
916 int again = KBD_MAXWAIT;
917 int c = KBD_DIAG_FAIL;
919 while (retry-- > 0) {
920 empty_both_buffers(p, 10);
921 if (write_controller_command(p, KBDC_DIAGNOSE))
927 emptyq(&kbdcp(p)->kbd);
928 while (again-- > 0) {
930 DELAY(KBD_RESETDELAY*1000);
931 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
932 if (c != -1) /* wait again if the controller is not ready */
935 if (verbose || bootverbose)
936 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
937 return (c == KBD_DIAG_DONE);
941 test_kbd_port(KBDC p)
943 int retry = KBD_MAXRETRY;
944 int again = KBD_MAXWAIT;
947 while (retry-- > 0) {
948 empty_both_buffers(p, 10);
949 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
955 emptyq(&kbdcp(p)->kbd);
956 while (again-- > 0) {
957 c = read_controller_data(p);
958 if (c != -1) /* try again if the controller is not ready */
961 if (verbose || bootverbose)
962 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
967 test_aux_port(KBDC p)
969 int retry = KBD_MAXRETRY;
970 int again = KBD_MAXWAIT;
973 while (retry-- > 0) {
974 empty_both_buffers(p, 10);
975 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
981 emptyq(&kbdcp(p)->kbd);
982 while (again-- > 0) {
983 c = read_controller_data(p);
984 if (c != -1) /* try again if the controller is not ready */
987 if (verbose || bootverbose)
988 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
993 kbdc_get_device_mask(KBDC p)
995 return kbdcp(p)->command_mask;
999 kbdc_set_device_mask(KBDC p, int mask)
1001 kbdcp(p)->command_mask =
1002 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1006 get_controller_command_byte(KBDC p)
1008 if (kbdcp(p)->command_byte != -1)
1009 return kbdcp(p)->command_byte;
1010 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1012 emptyq(&kbdcp(p)->kbd);
1013 kbdcp(p)->command_byte = read_controller_data(p);
1014 return kbdcp(p)->command_byte;
1018 set_controller_command_byte(KBDC p, int mask, int command)
1020 if (get_controller_command_byte(p) == -1)
1023 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1024 if (command & KBD_DISABLE_KBD_PORT) {
1025 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1028 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1030 if (!write_controller_data(p, command))
1032 kbdcp(p)->command_byte = command;
1035 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",