Do a major clean-up of the BUSDMA architecture. A large number of
[dfdiff.git] / sys / dev / misc / kbd / atkbdc.c
blob145afb5e777c6530b2e4b493dfd6cee9e80ed1b8
1 /*-
2 * Copyright (c) 1996-1999
3 * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
16 * permission.
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
28 * SUCH DAMAGE.
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.8 2006/10/25 20:55:54 dillon Exp $
32 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
35 #include "opt_kbd.h"
36 #include "use_atkbdc.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/malloc.h>
42 #include <sys/syslog.h>
43 #include <sys/rman.h>
45 #include <machine/clock.h>
47 #include "atkbdcreg.h"
49 #include <bus/isa/isareg.h>
51 /* constants */
53 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
55 /* macros */
57 #ifndef MAX
58 #define MAX(x, y) ((x) > (y) ? (x) : (y))
59 #endif
61 #define kbdcp(p) ((atkbdc_softc_t *)(p))
62 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
63 #define availq(q) ((q)->head != (q)->tail)
64 #if KBDIO_DEBUG >= 2
65 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
66 #else
67 #define emptyq(q) ((q)->tail = (q)->head = 0)
68 #endif
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)))
77 /* local variables */
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(kqueue *q, int c);
95 static int removeq(kqueue *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);
103 atkbdc_softc_t *
104 atkbdc_get_softc(int unit)
106 atkbdc_softc_t *sc;
108 if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
109 return NULL;
110 sc = atkbdc_softc[unit];
111 if (sc == NULL) {
112 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
113 atkbdc_softc[unit] = sc;
115 return sc;
119 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
121 if (rman_get_start(port0) <= 0)
122 return ENXIO;
123 if (rman_get_start(port1) <= 0)
124 return ENXIO;
125 return 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)
141 bus_space_tag_t tag;
142 bus_space_handle_t h0;
143 bus_space_handle_t h1;
144 int port0;
145 int port1;
147 port0 = IO_KBD;
148 resource_int_value("atkbdc", 0, "port", &port0);
149 port1 = IO_KBD + KBD_STATUS_PORT;
150 #if 0
151 resource_int_value("atkbdc", 0, "port", &port0);
152 #endif
154 /* XXX: tag should be passed from the caller */
155 #if defined(__i386__)
156 tag = I386_BUS_SPACE_IO;
157 #endif
159 #if notyet
160 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
161 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
162 #else
163 h0 = (bus_space_handle_t)port0;
164 h1 = (bus_space_handle_t)port1;
165 #endif
166 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
169 static int
170 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
171 bus_space_handle_t h1)
173 if (sc->ioh0 == 0) { /* XXX */
174 sc->command_byte = -1;
175 sc->command_mask = 0;
176 sc->lock = FALSE;
177 sc->kbd.head = sc->kbd.tail = 0;
178 sc->aux.head = sc->aux.tail = 0;
179 #if KBDIO_DEBUG >= 2
180 sc->kbd.call_count = 0;
181 sc->kbd.qcount = sc->kbd.max_qcount = 0;
182 sc->aux.call_count = 0;
183 sc->aux.qcount = sc->aux.max_qcount = 0;
184 #endif
186 sc->iot = tag;
187 sc->ioh0 = h0;
188 sc->ioh1 = h1;
189 return 0;
192 /* open a keyboard controller */
193 KBDC
194 atkbdc_open(int unit)
196 if (unit <= 0)
197 unit = 0;
198 if (unit >= MAXKBDC)
199 return NULL;
200 if ((atkbdc_softc[unit]->port0 != NULL)
201 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */
202 return (KBDC)atkbdc_softc[unit];
203 return NULL;
207 * I/O access arbitration in `kbdio'
209 * The `kbdio' module uses a simplistic convention to arbitrate
210 * I/O access to the controller/keyboard/mouse. The convention requires
211 * close cooperation of the calling device driver.
213 * The device drivers which utilize the `kbdio' module are assumed to
214 * have the following set of routines.
215 * a. An interrupt handler (the bottom half of the driver).
216 * b. Timeout routines which may briefly poll the keyboard controller.
217 * c. Routines outside interrupt context (the top half of the driver).
218 * They should follow the rules below:
219 * 1. The interrupt handler may assume that it always has full access
220 * to the controller/keyboard/mouse.
221 * 2. The other routines must issue `spltty()' if they wish to
222 * prevent the interrupt handler from accessing
223 * the controller/keyboard/mouse.
224 * 3. The timeout routines and the top half routines of the device driver
225 * arbitrate I/O access by observing the lock flag in `kbdio'.
226 * The flag is manipulated via `kbdc_lock()'; when one wants to
227 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
228 * the call returns with TRUE. Otherwise the caller must back off.
229 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
230 * is finished. This mechanism does not prevent the interrupt
231 * handler from being invoked at any time and carrying out I/O.
232 * Therefore, `spltty()' must be strategically placed in the device
233 * driver code. Also note that the timeout routine may interrupt
234 * `kbdc_lock()' called by the top half of the driver, but this
235 * interruption is OK so long as the timeout routine observes
236 * rule 4 below.
237 * 4. The interrupt and timeout routines should not extend I/O operation
238 * across more than one interrupt or timeout; they must complete any
239 * necessary I/O operation within one invocation of the routine.
240 * This means that if the timeout routine acquires the lock flag,
241 * it must reset the flag to FALSE before it returns.
244 /* set/reset polling lock */
245 int
246 kbdc_lock(KBDC p, int lock)
248 int prevlock;
250 prevlock = kbdcp(p)->lock;
251 kbdcp(p)->lock = lock;
253 return (prevlock != lock);
256 /* check if any data is waiting to be processed */
258 kbdc_data_ready(KBDC p)
260 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
261 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
264 /* queuing functions */
266 static int
267 addq(kqueue *q, int c)
269 if (nextq(q->tail) != q->head) {
270 q->q[q->tail] = c;
271 q->tail = nextq(q->tail);
272 #if KBDIO_DEBUG >= 2
273 ++q->call_count;
274 ++q->qcount;
275 if (q->qcount > q->max_qcount)
276 q->max_qcount = q->qcount;
277 #endif
278 return TRUE;
280 return FALSE;
283 static int
284 removeq(kqueue *q)
286 int c;
288 if (q->tail != q->head) {
289 c = q->q[q->head];
290 q->head = nextq(q->head);
291 #if KBDIO_DEBUG >= 2
292 --q->qcount;
293 #endif
294 return c;
296 return -1;
300 * device I/O routines
302 static int
303 wait_while_controller_busy(struct atkbdc_softc *kbdc)
305 /* CPU will stay inside the loop for 100msec at most */
306 int retry = 5000;
307 int f;
309 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
310 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
311 DELAY(KBDD_DELAYTIME);
312 addq(&kbdc->kbd, read_data(kbdc));
313 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
314 DELAY(KBDD_DELAYTIME);
315 addq(&kbdc->aux, read_data(kbdc));
317 DELAY(KBDC_DELAYTIME);
318 if (--retry < 0)
319 return FALSE;
321 return TRUE;
325 * wait for any data; whether it's from the controller,
326 * the keyboard, or the aux device.
328 static int
329 wait_for_data(struct atkbdc_softc *kbdc)
331 /* CPU will stay inside the loop for 200msec at most */
332 int retry = 10000;
333 int f;
335 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
336 DELAY(KBDC_DELAYTIME);
337 if (--retry < 0)
338 return 0;
340 DELAY(KBDD_DELAYTIME);
341 return f;
344 /* wait for data from the keyboard */
345 static int
346 wait_for_kbd_data(struct atkbdc_softc *kbdc)
348 /* CPU will stay inside the loop for 200msec at most */
349 int retry = 10000;
350 int f;
352 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
353 != KBDS_KBD_BUFFER_FULL) {
354 if (f == KBDS_AUX_BUFFER_FULL) {
355 DELAY(KBDD_DELAYTIME);
356 addq(&kbdc->aux, read_data(kbdc));
358 DELAY(KBDC_DELAYTIME);
359 if (--retry < 0)
360 return 0;
362 DELAY(KBDD_DELAYTIME);
363 return f;
367 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
368 * queue anything else.
370 static int
371 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
373 /* CPU will stay inside the loop for 200msec at most */
374 int retry = 10000;
375 int f;
376 int b;
378 while (retry-- > 0) {
379 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
380 DELAY(KBDD_DELAYTIME);
381 b = read_data(kbdc);
382 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
383 if ((b == KBD_ACK) || (b == KBD_RESEND)
384 || (b == KBD_RESET_FAIL))
385 return b;
386 addq(&kbdc->kbd, b);
387 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
388 addq(&kbdc->aux, b);
391 DELAY(KBDC_DELAYTIME);
393 return -1;
396 /* wait for data from the aux device */
397 static int
398 wait_for_aux_data(struct atkbdc_softc *kbdc)
400 /* CPU will stay inside the loop for 200msec at most */
401 int retry = 10000;
402 int f;
404 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
405 != KBDS_AUX_BUFFER_FULL) {
406 if (f == KBDS_KBD_BUFFER_FULL) {
407 DELAY(KBDD_DELAYTIME);
408 addq(&kbdc->kbd, read_data(kbdc));
410 DELAY(KBDC_DELAYTIME);
411 if (--retry < 0)
412 return 0;
414 DELAY(KBDD_DELAYTIME);
415 return f;
419 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
420 * queue anything else.
422 static int
423 wait_for_aux_ack(struct atkbdc_softc *kbdc)
425 /* CPU will stay inside the loop for 200msec at most */
426 int retry = 10000;
427 int f;
428 int b;
430 while (retry-- > 0) {
431 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
432 DELAY(KBDD_DELAYTIME);
433 b = read_data(kbdc);
434 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
435 if ((b == PSM_ACK) || (b == PSM_RESEND)
436 || (b == PSM_RESET_FAIL))
437 return b;
438 addq(&kbdc->aux, b);
439 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
440 addq(&kbdc->kbd, b);
443 DELAY(KBDC_DELAYTIME);
445 return -1;
448 /* write a one byte command to the controller */
450 write_controller_command(KBDC p, int c)
452 if (!wait_while_controller_busy(kbdcp(p)))
453 return FALSE;
454 write_command(kbdcp(p), c);
455 return TRUE;
458 /* write a one byte data to the controller */
460 write_controller_data(KBDC p, int c)
462 if (!wait_while_controller_busy(kbdcp(p)))
463 return FALSE;
464 write_data(kbdcp(p), c);
465 return TRUE;
468 /* write a one byte keyboard command */
470 write_kbd_command(KBDC p, int c)
472 if (!wait_while_controller_busy(kbdcp(p)))
473 return FALSE;
474 write_data(kbdcp(p), c);
475 return TRUE;
478 /* write a one byte auxiliary device command */
480 write_aux_command(KBDC p, int c)
482 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
483 return FALSE;
484 return write_controller_data(p, c);
487 /* send a command to the keyboard and wait for ACK */
489 send_kbd_command(KBDC p, int c)
491 int retry = KBD_MAXRETRY;
492 int res = -1;
494 while (retry-- > 0) {
495 if (!write_kbd_command(p, c))
496 continue;
497 res = wait_for_kbd_ack(kbdcp(p));
498 if (res == KBD_ACK)
499 break;
501 return res;
504 /* send a command to the auxiliary device and wait for ACK */
506 send_aux_command(KBDC p, int c)
508 int retry = KBD_MAXRETRY;
509 int res = -1;
511 while (retry-- > 0) {
512 if (!write_aux_command(p, c))
513 continue;
515 * FIXME: XXX
516 * The aux device may have already sent one or two bytes of
517 * status data, when a command is received. It will immediately
518 * stop data transmission, thus, leaving an incomplete data
519 * packet in our buffer. We have to discard any unprocessed
520 * data in order to remove such packets. Well, we may remove
521 * unprocessed, but necessary data byte as well...
523 emptyq(&kbdcp(p)->aux);
524 res = wait_for_aux_ack(kbdcp(p));
525 if (res == PSM_ACK)
526 break;
528 return res;
531 /* send a command and a data to the keyboard, wait for ACKs */
533 send_kbd_command_and_data(KBDC p, int c, int d)
535 int retry;
536 int res = -1;
538 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
539 if (!write_kbd_command(p, c))
540 continue;
541 res = wait_for_kbd_ack(kbdcp(p));
542 if (res == KBD_ACK)
543 break;
544 else if (res != KBD_RESEND)
545 return res;
547 if (retry <= 0)
548 return res;
550 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
551 if (!write_kbd_command(p, d))
552 continue;
553 res = wait_for_kbd_ack(kbdcp(p));
554 if (res != KBD_RESEND)
555 break;
557 return res;
560 /* send a command and a data to the auxiliary device, wait for ACKs */
562 send_aux_command_and_data(KBDC p, int c, int d)
564 int retry;
565 int res = -1;
567 for (retry = KBD_MAXRETRY; retry > 0; --retry) {
568 if (!write_aux_command(p, c))
569 continue;
570 emptyq(&kbdcp(p)->aux);
571 res = wait_for_aux_ack(kbdcp(p));
572 if (res == PSM_ACK)
573 break;
574 else if (res != PSM_RESEND)
575 return res;
577 if (retry <= 0)
578 return res;
580 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
581 if (!write_aux_command(p, d))
582 continue;
583 res = wait_for_aux_ack(kbdcp(p));
584 if (res != PSM_RESEND)
585 break;
587 return res;
591 * read one byte from any source; whether from the controller,
592 * the keyboard, or the aux device
595 read_controller_data(KBDC p)
597 if (availq(&kbdcp(p)->kbd))
598 return removeq(&kbdcp(p)->kbd);
599 if (availq(&kbdcp(p)->aux))
600 return removeq(&kbdcp(p)->aux);
601 if (!wait_for_data(kbdcp(p)))
602 return -1; /* timeout */
603 return read_data(kbdcp(p));
606 #if KBDIO_DEBUG >= 2
607 static int call = 0;
608 #endif
610 /* read one byte from the keyboard */
612 read_kbd_data(KBDC p)
614 #if KBDIO_DEBUG >= 2
615 if (++call > 2000) {
616 call = 0;
617 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
618 "aux q: %d calls, max %d chars\n",
619 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
620 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
622 #endif
624 if (availq(&kbdcp(p)->kbd))
625 return removeq(&kbdcp(p)->kbd);
626 if (!wait_for_kbd_data(kbdcp(p)))
627 return -1; /* timeout */
628 return read_data(kbdcp(p));
631 /* read one byte from the keyboard, but return immediately if
632 * no data is waiting
635 read_kbd_data_no_wait(KBDC p)
637 int f;
639 #if KBDIO_DEBUG >= 2
640 if (++call > 2000) {
641 call = 0;
642 log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
643 "aux q: %d calls, max %d chars\n",
644 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
645 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
647 #endif
649 if (availq(&kbdcp(p)->kbd))
650 return removeq(&kbdcp(p)->kbd);
651 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
652 if (f == KBDS_AUX_BUFFER_FULL) {
653 DELAY(KBDD_DELAYTIME);
654 addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
655 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
657 if (f == KBDS_KBD_BUFFER_FULL) {
658 DELAY(KBDD_DELAYTIME);
659 return read_data(kbdcp(p));
661 return -1; /* no data */
664 /* read one byte from the aux device */
666 read_aux_data(KBDC p)
668 if (availq(&kbdcp(p)->aux))
669 return removeq(&kbdcp(p)->aux);
670 if (!wait_for_aux_data(kbdcp(p)))
671 return -1; /* timeout */
672 return read_data(kbdcp(p));
675 /* read one byte from the aux device, but return immediately if
676 * no data is waiting
679 read_aux_data_no_wait(KBDC p)
681 int f;
683 if (availq(&kbdcp(p)->aux))
684 return removeq(&kbdcp(p)->aux);
685 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
686 if (f == KBDS_KBD_BUFFER_FULL) {
687 DELAY(KBDD_DELAYTIME);
688 addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
689 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
691 if (f == KBDS_AUX_BUFFER_FULL) {
692 DELAY(KBDD_DELAYTIME);
693 return read_data(kbdcp(p));
695 return -1; /* no data */
698 /* discard data from the keyboard */
699 void
700 empty_kbd_buffer(KBDC p, int wait)
702 int t;
703 int b;
704 int f;
705 #if KBDIO_DEBUG >= 2
706 int c1 = 0;
707 int c2 = 0;
708 #endif
709 int delta = 2;
711 for (t = wait; t > 0; ) {
712 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
713 DELAY(KBDD_DELAYTIME);
714 b = read_data(kbdcp(p));
715 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
716 addq(&kbdcp(p)->aux, b);
717 #if KBDIO_DEBUG >= 2
718 ++c2;
719 } else {
720 ++c1;
721 #endif
723 t = wait;
724 } else {
725 t -= delta;
727 DELAY(delta*1000);
729 #if KBDIO_DEBUG >= 2
730 if ((c1 > 0) || (c2 > 0))
731 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
732 #endif
734 emptyq(&kbdcp(p)->kbd);
737 /* discard data from the aux device */
738 void
739 empty_aux_buffer(KBDC p, int wait)
741 int t;
742 int b;
743 int f;
744 #if KBDIO_DEBUG >= 2
745 int c1 = 0;
746 int c2 = 0;
747 #endif
748 int delta = 2;
750 for (t = wait; t > 0; ) {
751 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
752 DELAY(KBDD_DELAYTIME);
753 b = read_data(kbdcp(p));
754 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
755 addq(&kbdcp(p)->kbd, b);
756 #if KBDIO_DEBUG >= 2
757 ++c1;
758 } else {
759 ++c2;
760 #endif
762 t = wait;
763 } else {
764 t -= delta;
766 DELAY(delta*1000);
768 #if KBDIO_DEBUG >= 2
769 if ((c1 > 0) || (c2 > 0))
770 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
771 #endif
773 emptyq(&kbdcp(p)->aux);
776 /* discard any data from the keyboard or the aux device */
777 void
778 empty_both_buffers(KBDC p, int wait)
780 int t;
781 int f;
782 #if KBDIO_DEBUG >= 2
783 int c1 = 0;
784 int c2 = 0;
785 #endif
786 int delta = 2;
788 for (t = wait; t > 0; ) {
789 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
790 DELAY(KBDD_DELAYTIME);
791 (void)read_data(kbdcp(p));
792 #if KBDIO_DEBUG >= 2
793 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
794 ++c1;
795 else
796 ++c2;
797 #endif
798 t = wait;
799 } else {
800 t -= delta;
802 DELAY(delta*1000);
804 #if KBDIO_DEBUG >= 2
805 if ((c1 > 0) || (c2 > 0))
806 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
807 #endif
809 emptyq(&kbdcp(p)->kbd);
810 emptyq(&kbdcp(p)->aux);
813 /* keyboard and mouse device control */
815 /* NOTE: enable the keyboard port but disable the keyboard
816 * interrupt before calling "reset_kbd()".
819 reset_kbd(KBDC p)
821 int retry = KBD_MAXRETRY;
822 int again = KBD_MAXWAIT;
823 int c = KBD_RESEND; /* keep the compiler happy */
825 while (retry-- > 0) {
826 empty_both_buffers(p, 10);
827 if (!write_kbd_command(p, KBDC_RESET_KBD))
828 continue;
829 emptyq(&kbdcp(p)->kbd);
830 c = read_controller_data(p);
831 if (verbose || bootverbose)
832 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
833 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
834 break;
836 if (retry < 0)
837 return FALSE;
839 while (again-- > 0) {
840 /* wait awhile, well, in fact we must wait quite loooooooooooong */
841 DELAY(KBD_RESETDELAY*1000);
842 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */
843 if (c != -1) /* wait again if the controller is not ready */
844 break;
846 if (verbose || bootverbose)
847 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
848 if (c != KBD_RESET_DONE)
849 return FALSE;
850 return TRUE;
853 /* NOTE: enable the aux port but disable the aux interrupt
854 * before calling `reset_aux_dev()'.
857 reset_aux_dev(KBDC p)
859 int retry = KBD_MAXRETRY;
860 int again = KBD_MAXWAIT;
861 int c = PSM_RESEND; /* keep the compiler happy */
863 while (retry-- > 0) {
864 empty_both_buffers(p, 10);
865 if (!write_aux_command(p, PSMC_RESET_DEV))
866 continue;
867 emptyq(&kbdcp(p)->aux);
868 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
869 for (again = KBD_MAXWAIT; again > 0; --again) {
870 DELAY(KBD_RESETDELAY*1000);
871 c = read_aux_data_no_wait(p);
872 if (c != -1)
873 break;
875 if (verbose || bootverbose)
876 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
877 if (c == PSM_ACK) /* aux dev is about to reset... */
878 break;
880 if (retry < 0)
881 return FALSE;
883 for (again = KBD_MAXWAIT; again > 0; --again) {
884 /* wait awhile, well, quite looooooooooooong */
885 DELAY(KBD_RESETDELAY*1000);
886 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */
887 if (c != -1) /* wait again if the controller is not ready */
888 break;
890 if (verbose || bootverbose)
891 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
892 if (c != PSM_RESET_DONE) /* reset status */
893 return FALSE;
895 c = read_aux_data(p); /* device ID */
896 if (verbose || bootverbose)
897 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
898 /* NOTE: we could check the device ID now, but leave it later... */
899 return TRUE;
902 /* controller diagnostics and setup */
905 test_controller(KBDC p)
907 int retry = KBD_MAXRETRY;
908 int again = KBD_MAXWAIT;
909 int c = KBD_DIAG_FAIL;
911 while (retry-- > 0) {
912 empty_both_buffers(p, 10);
913 if (write_controller_command(p, KBDC_DIAGNOSE))
914 break;
916 if (retry < 0)
917 return FALSE;
919 emptyq(&kbdcp(p)->kbd);
920 while (again-- > 0) {
921 /* wait awhile */
922 DELAY(KBD_RESETDELAY*1000);
923 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */
924 if (c != -1) /* wait again if the controller is not ready */
925 break;
927 if (verbose || bootverbose)
928 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
929 return (c == KBD_DIAG_DONE);
933 test_kbd_port(KBDC p)
935 int retry = KBD_MAXRETRY;
936 int again = KBD_MAXWAIT;
937 int c = -1;
939 while (retry-- > 0) {
940 empty_both_buffers(p, 10);
941 if (write_controller_command(p, KBDC_TEST_KBD_PORT))
942 break;
944 if (retry < 0)
945 return FALSE;
947 emptyq(&kbdcp(p)->kbd);
948 while (again-- > 0) {
949 c = read_controller_data(p);
950 if (c != -1) /* try again if the controller is not ready */
951 break;
953 if (verbose || bootverbose)
954 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
955 return c;
959 test_aux_port(KBDC p)
961 int retry = KBD_MAXRETRY;
962 int again = KBD_MAXWAIT;
963 int c = -1;
965 while (retry-- > 0) {
966 empty_both_buffers(p, 10);
967 if (write_controller_command(p, KBDC_TEST_AUX_PORT))
968 break;
970 if (retry < 0)
971 return FALSE;
973 emptyq(&kbdcp(p)->kbd);
974 while (again-- > 0) {
975 c = read_controller_data(p);
976 if (c != -1) /* try again if the controller is not ready */
977 break;
979 if (verbose || bootverbose)
980 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
981 return c;
985 kbdc_get_device_mask(KBDC p)
987 return kbdcp(p)->command_mask;
990 void
991 kbdc_set_device_mask(KBDC p, int mask)
993 kbdcp(p)->command_mask =
994 mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
998 get_controller_command_byte(KBDC p)
1000 if (kbdcp(p)->command_byte != -1)
1001 return kbdcp(p)->command_byte;
1002 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1003 return -1;
1004 emptyq(&kbdcp(p)->kbd);
1005 kbdcp(p)->command_byte = read_controller_data(p);
1006 return kbdcp(p)->command_byte;
1010 set_controller_command_byte(KBDC p, int mask, int command)
1012 if (get_controller_command_byte(p) == -1)
1013 return FALSE;
1015 command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1016 if (command & KBD_DISABLE_KBD_PORT) {
1017 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1018 return FALSE;
1020 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1021 return FALSE;
1022 if (!write_controller_data(p, command))
1023 return FALSE;
1024 kbdcp(p)->command_byte = command;
1026 if (verbose)
1027 log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",
1028 command);
1030 return TRUE;