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 * $DragonFly: src/sys/dev/misc/kbd/atkbdc.c,v 1.11 2007/04/22 10:54:43 y0netan1 Exp $
34 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
38 #include "use_atkbdc.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/syslog.h>
47 #include <machine/clock.h>
49 #include "atkbdcreg.h"
51 #include <bus/isa/isareg.h>
54 #define lwkt_gettoken(x)
55 #define lwkt_reltoken(x)
60 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
65 #define MAX(x, y) ((x) > (y) ? (x) : (y))
68 #define kbdcp(p) ((atkbdc_softc_t *)(p))
69 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
70 #define availq(q) ((q)->head != (q)->tail)
72 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
74 #define emptyq(q) ((q)->tail = (q)->head = 0)
77 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0))
78 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0))
79 #define write_data(k, d) \
80 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
81 #define write_command(k, d) \
82 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
87 * We always need at least one copy of the kbdc_softc struct for the
88 * low-level console. As the low-level console accesses the keyboard
89 * controller before kbdc, and all other devices, is probed, we
90 * statically allocate one entry. XXX
92 static atkbdc_softc_t default_kbdc;
93 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
95 static int verbose = KBDIO_DEBUG;
97 /* function prototypes */
99 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
100 bus_space_handle_t h0, bus_space_handle_t h1);
101 static int addq(kbdkqueue *q, int c);
102 static int removeq(kbdkqueue *q);
103 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
104 static int wait_for_data(atkbdc_softc_t *kbdc);
105 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
106 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
107 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
108 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
111 atkbdc_get_softc(int unit)
115 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
117 sc = atkbdc_softc[unit];
119 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
120 atkbdc_softc[unit] = sc;
126 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
128 if (rman_get_start(port0) <= 0)
130 if (rman_get_start(port1) <= 0)
136 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
137 struct resource *port1)
139 return atkbdc_setup(sc, rman_get_bustag(port0),
140 rman_get_bushandle(port0),
141 rman_get_bushandle(port1));
144 /* the backdoor to the keyboard controller! XXX */
146 atkbdc_configure(void)
149 bus_space_handle_t h0;
150 bus_space_handle_t h1;
153 #if defined(__i386__) || defined(__x86_64__)
158 resource_int_value("atkbdc", 0, "port", &port0);
159 port1 = IO_KBD + KBD_STATUS_PORT;
161 resource_int_value("atkbdc", 0, "port", &port0);
164 /* XXX: tag should be passed from the caller */
165 #if defined(__i386__)
166 tag = I386_BUS_SPACE_IO;
172 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
173 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
175 h0 = (bus_space_handle_t)port0;
176 h1 = (bus_space_handle_t)port1;
179 #if defined(__i386__) || defined(__x86_64__)
181 * Check if we really have AT keyboard controller. Poll status
182 * register until we get "all clear" indication. If no such
183 * indication comes, it probably means that there is no AT
184 * keyboard controller present. Give up in such case. Check relies
185 * on the fact that reading from non-existing in/out port returns
186 * 0xff on i386. May or may not be true on other platforms.
188 for (i = 65536; i != 0; --i) {
189 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
197 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
201 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
202 bus_space_handle_t h1)
204 if (sc->ioh0 == 0) { /* XXX */
205 sc->command_byte = -1;
206 sc->command_mask = 0;
208 sc->kbd.head = sc->kbd.tail = 0;
209 sc->aux.head = sc->aux.tail = 0;
211 sc->kbd.call_count = 0;
212 sc->kbd.qcount = sc->kbd.max_qcount = 0;
213 sc->aux.call_count = 0;
214 sc->aux.qcount = sc->aux.max_qcount = 0;
223 /* open a keyboard controller */
225 atkbdc_open(int unit)
231 if ((atkbdc_softc[unit]->port0 != NULL)
232 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
233 return (KBDC)atkbdc_softc[unit];
238 * I/O access arbitration in `kbdio'
240 * The `kbdio' module uses a simplistic convention to arbitrate
241 * I/O access to the controller/keyboard/mouse. The convention requires
242 * close cooperation of the calling device driver.
244 * The device drivers which utilize the `kbdio' module are assumed to
245 * have the following set of routines.
246 * a. An interrupt handler (the bottom half of the driver).
247 * b. Timeout routines which may briefly poll the keyboard controller.
248 * c. Routines outside interrupt context (the top half of the driver).
249 * They should follow the rules below:
250 * 1. The interrupt handler may assume that it always has full access
251 * to the controller/keyboard/mouse.
252 * 2. The other routines must issue `spltty()' if they wish to
253 * prevent the interrupt handler from accessing
254 * the controller/keyboard/mouse.
255 * 3. The timeout routines and the top half routines of the device driver
256 * arbitrate I/O access by observing the lock flag in `kbdio'.
257 * The flag is manipulated via `kbdc_lock()'; when one wants to
258 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
259 * the call returns with TRUE. Otherwise the caller must back off.
260 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
261 * is finished. This mechanism does not prevent the interrupt
262 * handler from being invoked at any time and carrying out I/O.
263 * Therefore, `spltty()' must be strategically placed in the device
264 * driver code. Also note that the timeout routine may interrupt
265 * `kbdc_lock()' called by the top half of the driver, but this
266 * interruption is OK so long as the timeout routine observes
268 * 4. The interrupt and timeout routines should not extend I/O operation
269 * across more than one interrupt or timeout; they must complete any
270 * necessary I/O operation within one invocation of the routine.
271 * This means that if the timeout routine acquires the lock flag,
272 * it must reset the flag to FALSE before it returns.
275 /* set/reset polling lock */
277 kbdc_lock(KBDC p, int lock)
281 prevlock = kbdcp(p)->lock;
282 kbdcp(p)->lock = lock;
284 return (prevlock != lock);
287 /* check if any data is waiting to be processed */
289 kbdc_data_ready(KBDC p)
291 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
292 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
295 /* queuing functions */
298 addq(kbdkqueue *q, int c)
300 if (nextq(q->tail) != q->head) {
302 q->tail = nextq(q->tail);
306 if (q->qcount > q->max_qcount)
307 q->max_qcount = q->qcount;
315 removeq(kbdkqueue *q)
319 if (q->tail != q->head) {
321 q->head = nextq(q->head);
331 * device I/O routines
334 wait_while_controller_busy(struct atkbdc_softc *kbdc)
336 /* CPU will stay inside the loop for 100msec at most */
337 TOTALDELAY retry = { .us = 70000, .last_clock =0 }; /* 70ms */
340 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
341 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
342 DELAY(KBDD_DELAYTIME);
343 addq(&kbdc->kbd, read_data(kbdc));
344 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
345 DELAY(KBDD_DELAYTIME);
346 addq(&kbdc->aux, read_data(kbdc));
348 DELAY(KBDC_DELAYTIME);
349 if (CHECKTIMEOUT(&retry))
356 * wait for any data; whether it's from the controller,
357 * the keyboard, or the aux device.
360 wait_for_data(struct atkbdc_softc *kbdc)
362 /* CPU will stay inside the loop for 200msec at most */
363 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
366 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
367 DELAY(KBDC_DELAYTIME);
368 if (CHECKTIMEOUT(&retry))
371 DELAY(KBDD_DELAYTIME);
375 /* wait for data from the keyboard */
377 wait_for_kbd_data(struct atkbdc_softc *kbdc)
379 /* CPU will stay inside the loop for 200msec at most */
380 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
383 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
384 != KBDS_KBD_BUFFER_FULL) {
385 if (f == KBDS_AUX_BUFFER_FULL) {
386 DELAY(KBDD_DELAYTIME);
387 addq(&kbdc->aux, read_data(kbdc));
389 DELAY(KBDC_DELAYTIME);
390 if (CHECKTIMEOUT(&retry))
393 DELAY(KBDD_DELAYTIME);
398 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
399 * queue anything else.
402 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
404 /* CPU will stay inside the loop for 200msec at most */
405 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
409 while (CHECKTIMEOUT(&retry) == 0) {
410 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
411 DELAY(KBDD_DELAYTIME);
413 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
414 if ((b == KBD_ACK) || (b == KBD_RESEND)
415 || (b == KBD_RESET_FAIL))
418 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
422 DELAY(KBDC_DELAYTIME);
427 /* wait for data from the aux device */
429 wait_for_aux_data(struct atkbdc_softc *kbdc)
431 /* CPU will stay inside the loop for 200msec at most */
432 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
435 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
436 != KBDS_AUX_BUFFER_FULL) {
437 if (f == KBDS_KBD_BUFFER_FULL) {
438 DELAY(KBDD_DELAYTIME);
439 addq(&kbdc->kbd, read_data(kbdc));
441 DELAY(KBDC_DELAYTIME);
442 if (CHECKTIMEOUT(&retry))
445 DELAY(KBDD_DELAYTIME);
450 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
451 * queue anything else.
454 wait_for_aux_ack(struct atkbdc_softc *kbdc)
456 /* CPU will stay inside the loop for 200msec at most */
457 TOTALDELAY retry = { 200000, 0 }; /* 200ms */
461 while (CHECKTIMEOUT(&retry) == 0) {
462 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
463 DELAY(KBDD_DELAYTIME);
465 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
466 if ((b == PSM_ACK) || (b == PSM_RESEND)
467 || (b == PSM_RESET_FAIL))
470 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
474 DELAY(KBDC_DELAYTIME);
479 /* write a one byte command to the controller */
481 write_controller_command(KBDC p, int c)
483 if (!wait_while_controller_busy(kbdcp(p)))
485 write_command(kbdcp(p), c);
489 /* write a one byte data to the controller */
491 write_controller_data(KBDC p, int c)
493 if (!wait_while_controller_busy(kbdcp(p)))
495 write_data(kbdcp(p), c);
499 /* write a one byte keyboard command */
501 write_kbd_command(KBDC p, int c)
503 if (!wait_while_controller_busy(kbdcp(p)))
505 write_data(kbdcp(p), c);
509 /* write a one byte auxiliary device command */
511 write_aux_command(KBDC p, int c)
513 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
515 return write_controller_data(p, c);
518 /* send a command to the keyboard and wait for ACK */
520 send_kbd_command(KBDC p, int c)
522 int retry = KBD_MAXRETRY;
525 while (retry-- > 0) {
526 if (!write_kbd_command(p, c))
528 res = wait_for_kbd_ack(kbdcp(p));
535 /* send a command to the auxiliary device and wait for ACK */
537 send_aux_command(KBDC p, int c)
539 int retry = KBD_MAXRETRY;
542 while (retry-- > 0) {
543 if (!write_aux_command(p, c))
547 * The aux device may have already sent one or two bytes of
548 * status data, when a command is received. It will immediately
549 * stop data transmission, thus, leaving an incomplete data
550 * packet in our buffer. We have to discard any unprocessed
551 * data in order to remove such packets. Well, we may remove
552 * unprocessed, but necessary data byte as well...
554 emptyq(&kbdcp(p)->aux);
555 res = wait_for_aux_ack(kbdcp(p));
562 /* send a command and a data to the keyboard, wait for ACKs */
564 send_kbd_command_and_data(KBDC p, int c, int d)
569 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
570 if (!write_kbd_command(p, c))
572 res = wait_for_kbd_ack(kbdcp(p));
575 else if (res != KBD_RESEND)
581 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
582 if (!write_kbd_command(p, d))
584 res = wait_for_kbd_ack(kbdcp(p));
585 if (res != KBD_RESEND)
591 /* send a command and a data to the auxiliary device, wait for ACKs */
593 send_aux_command_and_data(KBDC p, int c, int d)
598 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
599 if (!write_aux_command(p, c))
601 emptyq(&kbdcp(p)->aux);
602 res = wait_for_aux_ack(kbdcp(p));
605 else if (res != PSM_RESEND)
611 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
612 if (!write_aux_command(p, d))
614 res = wait_for_aux_ack(kbdcp(p));
615 if (res != PSM_RESEND)
622 * read one byte from any source; whether from the controller,
623 * the keyboard, or the aux device
626 read_controller_data(KBDC p)
628 if (availq(&kbdcp(p)->kbd))
629 return removeq(&kbdcp(p)->kbd);
630 if (availq(&kbdcp(p)->aux))
631 return removeq(&kbdcp(p)->aux);
632 if (!wait_for_data(kbdcp(p)))
633 return -1; /* timeout */
634 return read_data(kbdcp(p));
641 /* read one byte from the keyboard */
643 read_kbd_data(KBDC p)
648 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
649 "aux q: %d calls, max %d chars\n",
650 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
651 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
655 if (availq(&kbdcp(p)->kbd))
656 return removeq(&kbdcp(p)->kbd);
657 if (!wait_for_kbd_data(kbdcp(p)))
658 return -1; /* timeout */
659 return read_data(kbdcp(p));
662 /* read one byte from the keyboard, but return immediately if
666 read_kbd_data_no_wait(KBDC p)
673 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
674 "aux q: %d calls, max %d chars\n",
675 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
676 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
680 if (availq(&kbdcp(p)->kbd))
681 return removeq(&kbdcp(p)->kbd);
682 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
683 if (f == KBDS_AUX_BUFFER_FULL) {
684 DELAY(KBDD_DELAYTIME);
685 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
686 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
688 if (f == KBDS_KBD_BUFFER_FULL) {
689 DELAY(KBDD_DELAYTIME);
690 return read_data(kbdcp(p));
692 return -1; /* no data */
695 /* read one byte from the aux device */
697 read_aux_data(KBDC p)
699 if (availq(&kbdcp(p)->aux))
700 return removeq(&kbdcp(p)->aux);
701 if (!wait_for_aux_data(kbdcp(p)))
702 return -1; /* timeout */
703 return read_data(kbdcp(p));
706 /* read one byte from the aux device, but return immediately if
710 read_aux_data_no_wait(KBDC p)
714 if (availq(&kbdcp(p)->aux))
715 return removeq(&kbdcp(p)->aux);
716 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
717 if (f == KBDS_KBD_BUFFER_FULL) {
718 DELAY(KBDD_DELAYTIME);
719 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
720 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
722 if (f == KBDS_AUX_BUFFER_FULL) {
723 DELAY(KBDD_DELAYTIME);
724 return read_data(kbdcp(p));
726 return -1; /* no data */
729 /* discard data from the keyboard */
731 empty_kbd_buffer(KBDC p, int wait)
742 for (t = wait; t > 0; ) {
743 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
744 DELAY(KBDD_DELAYTIME);
745 b = read_data(kbdcp(p));
746 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
747 addq(&kbdcp(p)->aux, b);
761 if ((c1 > 0) || (c2 > 0))
762 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
765 emptyq(&kbdcp(p)->kbd);
768 /* discard data from the aux device */
770 empty_aux_buffer(KBDC p, int wait)
781 for (t = wait; t > 0; ) {
782 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
783 DELAY(KBDD_DELAYTIME);
784 b = read_data(kbdcp(p));
785 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
786 addq(&kbdcp(p)->kbd, b);
800 if ((c1 > 0) || (c2 > 0))
801 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
804 emptyq(&kbdcp(p)->aux);
807 /* discard any data from the keyboard or the aux device */
809 empty_both_buffers(KBDC p, int wait)
819 for (t = wait; t > 0; ) {
820 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
821 DELAY(KBDD_DELAYTIME);
822 (void)read_data(kbdcp(p));
824 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
836 if ((c1 > 0) || (c2 > 0))
837 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
840 emptyq(&kbdcp(p)->kbd);
841 emptyq(&kbdcp(p)->aux);
844 /* keyboard and mouse device control */
846 /* NOTE: enable the keyboard port but disable the keyboard
847 * interrupt before calling "reset_kbd()".
852 int retry = KBD_MAXRETRY;
853 int again = KBD_MAXWAIT;
854 int c = KBD_RESEND; /* keep the compiler happy */
856 while (retry-- > 0) {
857 empty_both_buffers(p, 10);
858 if (!write_kbd_command(p, KBDC_RESET_KBD))
860 emptyq(&kbdcp(p)->kbd);
861 c = read_controller_data(p);
862 if (verbose || bootverbose)
863 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
864 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
870 while (again-- > 0) {
871 /* wait awhile, well, in fact we must wait quite loooooooooooong */
872 DELAY(KBD_RESETDELAY*1000);
873 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
874 if (c != -1) /* wait again if the controller is not ready */
877 if (verbose || bootverbose)
878 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
879 if (c != KBD_RESET_DONE)
884 /* NOTE: enable the aux port but disable the aux interrupt
885 * before calling `reset_aux_dev()'.
888 reset_aux_dev(KBDC p)
890 int retry = KBD_MAXRETRY;
891 int again = KBD_MAXWAIT;
892 int c = PSM_RESEND; /* keep the compiler happy */
894 while (retry-- > 0) {
895 empty_both_buffers(p, 10);
896 if (!write_aux_command(p, PSMC_RESET_DEV))
898 emptyq(&kbdcp(p)->aux);
899 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
900 for (again = KBD_MAXWAIT; again > 0; --again) {
901 DELAY(KBD_RESETDELAY*1000);
902 c = read_aux_data_no_wait(p);
906 if (verbose || bootverbose)
907 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
908 if (c == PSM_ACK) /* aux dev is about to reset... */
914 for (again = KBD_MAXWAIT; again > 0; --again) {
915 /* wait awhile, well, quite looooooooooooong */
916 DELAY(KBD_RESETDELAY*1000);
917 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
918 if (c != -1) /* wait again if the controller is not ready */
921 if (verbose || bootverbose)
922 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
923 if (c != PSM_RESET_DONE) /* reset status */
926 c = read_aux_data(p); /* device ID */
927 if (verbose || bootverbose)
928 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
929 /* NOTE: we could check the device ID now, but leave it later... */
933 /* controller diagnostics and setup */
936 test_controller(KBDC p)
938 int retry = KBD_MAXRETRY;
939 int again = KBD_MAXWAIT;
940 int c = KBD_DIAG_FAIL;
942 while (retry-- > 0) {
943 empty_both_buffers(p, 10);
944 if (write_controller_command(p, KBDC_DIAGNOSE))
950 emptyq(&kbdcp(p)->kbd);
951 while (again-- > 0) {
953 DELAY(KBD_RESETDELAY*1000);
954 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
955 if (c != -1) /* wait again if the controller is not ready */
958 if (verbose || bootverbose)
959 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
960 return (c == KBD_DIAG_DONE);
964 test_kbd_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_KBD_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_KBD_PORT status:%04x\n", c);
990 test_aux_port(KBDC p)
992 int retry = KBD_MAXRETRY;
993 int again = KBD_MAXWAIT;
996 while (retry-- > 0) {
997 empty_both_buffers(p, 10);
998 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1004 emptyq(&kbdcp(p)->kbd);
1005 while (again-- > 0) {
1006 c = read_controller_data(p);
1007 if (c != -1) /* try again if the controller is not ready */
1010 if (verbose || bootverbose)
1011 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1016 kbdc_get_device_mask(KBDC p)
1018 return kbdcp(p)->command_mask;
1022 kbdc_set_device_mask(KBDC p, int mask)
1024 kbdcp(p)->command_mask =
1025 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1029 get_controller_command_byte(KBDC p)
1031 if (kbdcp(p)->command_byte != -1)
1032 return kbdcp(p)->command_byte;
1033 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1035 emptyq(&kbdcp(p)->kbd);
1036 kbdcp(p)->command_byte = read_controller_data(p);
1037 return kbdcp(p)->command_byte;
1041 set_controller_command_byte(KBDC p, int mask, int command)
1043 if (get_controller_command_byte(p) == -1)
1046 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1047 if (command & KBD_DISABLE_KBD_PORT) {
1048 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1051 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1053 if (!write_controller_data(p, command))
1055 kbdcp(p)->command_byte = command;
1058 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",