Merge branch 'vendor/MDOCML'
[dragonfly.git] / sys / dev / misc / kbd / atkbdc.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 1996-1999
5  * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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 
18  *    permission.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/dev/kbd/atkbdc.c,v 1.5.2.2 2002/03/31 11:02:02 murray Exp $
33  * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
34  */
35
36 #include "opt_kbd.h"
37 #include "use_atkbdc.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/malloc.h>
43 #include <sys/syslog.h>
44 #include <sys/rman.h>
45
46 #include <machine/clock.h>
47
48 #include "atkbdcreg.h"
49
50 #include <bus/isa/isareg.h>
51
52 #if 0
53 #define lwkt_gettoken(x)
54 #define lwkt_reltoken(x)
55 #endif
56
57 /* constants */
58
59 #define MAXKBDC         MAX(NATKBDC, 1)         /* XXX */
60
61 /* macros */
62
63 #ifndef MAX
64 #define MAX(x, y)       ((x) > (y) ? (x) : (y))
65 #endif
66
67 #define kbdcp(p)        ((atkbdc_softc_t *)(p))
68 #define nextq(i)        (((i) + 1) % KBDQ_BUFSIZE)
69 #define availq(q)       ((q)->head != (q)->tail)
70 #if KBDIO_DEBUG >= 2
71 #define emptyq(q)       ((q)->tail = (q)->head = (q)->qcount = 0)
72 #else
73 #define emptyq(q)       ((q)->tail = (q)->head = 0)
74 #endif
75
76 #define read_data(k)    (bus_space_read_1((k)->iot, (k)->ioh0, 0))
77 #define read_status(k)  (bus_space_read_1((k)->iot, (k)->ioh1, 0))
78 #define write_data(k, d)        \
79                         (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
80 #define write_command(k, d)     \
81                         (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
82
83 /* local variables */
84
85 /*
86  * We always need at least one copy of the kbdc_softc struct for the
87  * low-level console.  As the low-level console accesses the keyboard
88  * controller before kbdc, and all other devices, is probed, we
89  * statically allocate one entry. XXX
90  */
91 static atkbdc_softc_t default_kbdc;
92 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
93
94 static int verbose = KBDIO_DEBUG;
95
96 /* function prototypes */
97
98 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
99                         bus_space_handle_t h0, bus_space_handle_t h1);
100 static int addq(kbdkqueue *q, int c);
101 static int removeq(kbdkqueue *q);
102 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
103 static int wait_for_data(atkbdc_softc_t *kbdc);
104 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
105 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
106 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
107 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
108
109 atkbdc_softc_t *
110 atkbdc_get_softc(int unit)
111 {
112         atkbdc_softc_t *sc;
113
114         if (unit >= NELEM(atkbdc_softc))
115                 return NULL;
116         sc = atkbdc_softc[unit];
117         if (sc == NULL) {
118                 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
119                 atkbdc_softc[unit] = sc;
120         }
121         return sc;
122 }
123
124 int
125 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
126 {
127         if (rman_get_start(port0) <= 0)
128                 return ENXIO;
129         if (rman_get_start(port1) <= 0)
130                 return ENXIO;
131         return 0;
132 }
133
134 int
135 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
136                    struct resource *port1)
137 {
138         return atkbdc_setup(sc, rman_get_bustag(port0),
139                             rman_get_bushandle(port0),
140                             rman_get_bushandle(port1));
141 }
142
143 /* the backdoor to the keyboard controller! XXX */
144 int
145 atkbdc_configure(void)
146 {
147         bus_space_tag_t tag;
148         bus_space_handle_t h0;
149         bus_space_handle_t h1;
150         int port0;
151         int port1;
152 #if defined(__i386__) || defined(__x86_64__)
153         int i;
154 #endif
155
156         port0 = IO_KBD;
157         resource_int_value("atkbdc", 0, "port", &port0);
158         port1 = IO_KBD + KBD_STATUS_PORT;
159 #if 0
160         resource_int_value("atkbdc", 0, "port", &port0);
161 #endif
162
163         /* XXX: tag should be passed from the caller */
164 #if defined(__i386__)
165         tag = I386_BUS_SPACE_IO;
166 #else
167         tag = 0;        /* XXX */
168 #endif
169
170 #if 0 /* notyet */
171         bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
172         bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
173 #else
174         h0 = (bus_space_handle_t)port0;
175         h1 = (bus_space_handle_t)port1;
176 #endif
177
178 #if defined(__i386__) || defined(__x86_64__)
179         /*
180          * Check if we really have AT keyboard controller. Poll status
181          * register until we get "all clear" indication. If no such
182          * indication comes, it probably means that there is no AT
183          * keyboard controller present. Give up in such case. Check relies
184          * on the fact that reading from non-existing in/out port returns
185          * 0xff on i386. May or may not be true on other platforms.
186          */
187         for (i = 65536; i != 0; --i) {
188                 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
189                         break;
190                 DELAY(16);
191         }
192         if (i == 0)
193                 return ENXIO;
194 #endif
195
196         return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
197 }
198
199 static int
200 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
201              bus_space_handle_t h1)
202 {
203         if (sc->ioh0 == 0) {    /* XXX */
204             sc->command_byte = -1;
205             sc->lock = FALSE;
206             sc->kbd.head = sc->kbd.tail = 0;
207             sc->aux.head = sc->aux.tail = 0;
208 #if KBDIO_DEBUG >= 2
209             sc->kbd.call_count = 0;
210             sc->kbd.qcount = sc->kbd.max_qcount = 0;
211             sc->aux.call_count = 0;
212             sc->aux.qcount = sc->aux.max_qcount = 0;
213 #endif
214         }
215         sc->iot = tag;
216         sc->ioh0 = h0;
217         sc->ioh1 = h1;
218         return 0;
219 }
220
221 /* open a keyboard controller */
222 KBDC 
223 atkbdc_open(int unit)
224 {
225     if (unit <= 0)
226         unit = 0;
227     if (unit >= MAXKBDC)
228         return NULL;
229     if ((atkbdc_softc[unit]->port0 != NULL)
230         || (atkbdc_softc[unit]->ioh0 != 0))             /* XXX */
231         return (KBDC)atkbdc_softc[unit];
232     return NULL;
233 }
234
235 /*
236  * I/O access arbitration in `kbdio'
237  *
238  * The `kbdio' module uses a simplistic convention to arbitrate
239  * I/O access to the controller/keyboard/mouse. The convention requires
240  * close cooperation of the calling device driver.
241  *
242  * The device drivers which utilize the `kbdio' module are assumed to
243  * have the following set of routines.
244  *    a. An interrupt handler (the bottom half of the driver).
245  *    b. Timeout routines which may briefly poll the keyboard controller.
246  *    c. Routines outside interrupt context (the top half of the driver).
247  * They should follow the rules below:
248  *    1. The interrupt handler may assume that it always has full access 
249  *       to the controller/keyboard/mouse.
250  *    2. The other routines must issue `spltty()' if they wish to 
251  *       prevent the interrupt handler from accessing 
252  *       the controller/keyboard/mouse.
253  *    3. The timeout routines and the top half routines of the device driver
254  *       arbitrate I/O access by observing the lock flag in `kbdio'.
255  *       The flag is manipulated via `kbdc_lock()'; when one wants to
256  *       perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
257  *       the call returns with TRUE. Otherwise the caller must back off.
258  *       Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
259  *       is finished. This mechanism does not prevent the interrupt 
260  *       handler from being invoked at any time and carrying out I/O.
261  *       Therefore, `spltty()' must be strategically placed in the device
262  *       driver code. Also note that the timeout routine may interrupt
263  *       `kbdc_lock()' called by the top half of the driver, but this
264  *       interruption is OK so long as the timeout routine observes
265  *       rule 4 below.
266  *    4. The interrupt and timeout routines should not extend I/O operation
267  *       across more than one interrupt or timeout; they must complete any
268  *       necessary I/O operation within one invocation of the routine.
269  *       This means that if the timeout routine acquires the lock flag,
270  *       it must reset the flag to FALSE before it returns.
271  */
272
273 /* set/reset polling lock */
274 int 
275 kbdc_lock(KBDC p, int lock)
276 {
277     int prevlock;
278
279     prevlock = kbdcp(p)->lock;
280     kbdcp(p)->lock = lock;
281
282     return (prevlock != lock);
283 }
284
285 /* check if any data is waiting to be processed */
286 int
287 kbdc_data_ready(KBDC p)
288 {
289     return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux) 
290         || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL));
291 }
292
293 /* queuing functions */
294
295 static int
296 addq(kbdkqueue *q, int c)
297 {
298     if (nextq(q->tail) != q->head) {
299         q->q[q->tail] = c;
300         q->tail = nextq(q->tail);
301 #if KBDIO_DEBUG >= 2
302         ++q->call_count;
303         ++q->qcount;
304         if (q->qcount > q->max_qcount)
305             q->max_qcount = q->qcount;
306 #endif
307         return TRUE;
308     }
309     return FALSE;
310 }
311
312 static int
313 removeq(kbdkqueue *q)
314 {
315     int c;
316
317     if (q->tail != q->head) {
318         c = q->q[q->head];
319         q->head = nextq(q->head);
320 #if KBDIO_DEBUG >= 2
321         --q->qcount;
322 #endif
323         return c;
324     }
325     return -1;
326 }
327
328 /* 
329  * device I/O routines
330  */
331 static int
332 wait_while_controller_busy(struct atkbdc_softc *kbdc)
333 {
334     /* CPU will stay inside the loop for 100msec at most */
335     TOTALDELAY retry = { .us = 70000, .last_clock =0 }; /* 70ms */
336     int f;
337     unsigned char c;
338
339     while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
340         if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
341             DELAY(KBDD_DELAYTIME);
342             c = read_data(kbdc);
343             addq(&kbdc->kbd, c);
344         } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
345             DELAY(KBDD_DELAYTIME);
346             c = read_data(kbdc);
347             addq(&kbdc->aux, c);
348         }
349         DELAY(KBDC_DELAYTIME);
350         if (CHECKTIMEOUT(&retry))
351             return FALSE;
352     }
353     return TRUE;
354 }
355
356 /*
357  * wait for any data; whether it's from the controller, 
358  * the keyboard, or the aux device.
359  */
360 static int
361 wait_for_data(struct atkbdc_softc *kbdc)
362 {
363     /* CPU will stay inside the loop for 200msec at most */
364     TOTALDELAY retry = { 200000, 0 };   /* 200ms */
365     int f;
366
367     while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
368         DELAY(KBDC_DELAYTIME);
369         if (CHECKTIMEOUT(&retry))
370             return 0;
371     }
372     DELAY(KBDD_DELAYTIME);
373     return f;
374 }
375
376 /* wait for data from the keyboard */
377 static int
378 wait_for_kbd_data(struct atkbdc_softc *kbdc)
379 {
380     /* CPU will stay inside the loop for 200msec at most */
381     TOTALDELAY retry = { 200000, 0 };   /* 200ms */
382     int f;
383     unsigned char c;
384
385     while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
386             != KBDS_KBD_BUFFER_FULL) {
387         if (f == KBDS_AUX_BUFFER_FULL) {
388             DELAY(KBDD_DELAYTIME);
389             c = read_data(kbdc);
390             addq(&kbdc->aux, c);
391         }
392         DELAY(KBDC_DELAYTIME);
393         if (CHECKTIMEOUT(&retry))
394             return 0;
395     }
396     DELAY(KBDD_DELAYTIME);
397     return f;
398 }
399
400 /* 
401  * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
402  * queue anything else.
403  */
404 static int
405 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
406 {
407     /* CPU will stay inside the loop for 200msec at most */
408     TOTALDELAY retry = { 200000, 0 };   /* 200ms */
409     int f;
410     int b;
411
412     while (CHECKTIMEOUT(&retry) == 0) {
413         if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
414             DELAY(KBDD_DELAYTIME);
415             b = read_data(kbdc);
416             if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
417                 if ((b == KBD_ACK) || (b == KBD_RESEND) 
418                     || (b == KBD_RESET_FAIL))
419                     return b;
420                 addq(&kbdc->kbd, b);
421             } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
422                 addq(&kbdc->aux, b);
423             }
424         }
425         DELAY(KBDC_DELAYTIME);
426     }
427     return -1;
428 }
429
430 /* wait for data from the aux device */
431 static int
432 wait_for_aux_data(struct atkbdc_softc *kbdc)
433 {
434     /* CPU will stay inside the loop for 200msec at most */
435     TOTALDELAY retry = { 200000, 0 };   /* 200ms */
436     int f;
437     unsigned char b;
438
439     while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
440             != KBDS_AUX_BUFFER_FULL) {
441         if (f == KBDS_KBD_BUFFER_FULL) {
442             DELAY(KBDD_DELAYTIME);
443             b = read_data(kbdc);
444             addq(&kbdc->kbd, b);
445         }
446         DELAY(KBDC_DELAYTIME);
447         if (CHECKTIMEOUT(&retry))
448             return 0;
449     }
450     DELAY(KBDD_DELAYTIME);
451     return f;
452 }
453
454 /* 
455  * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
456  * queue anything else.
457  */
458 static int
459 wait_for_aux_ack(struct atkbdc_softc *kbdc)
460 {
461     /* CPU will stay inside the loop for 200msec at most */
462     TOTALDELAY retry = { 200000, 0 };   /* 200ms */
463     int f;
464     int b;
465
466     while (CHECKTIMEOUT(&retry) == 0) {
467         if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
468             DELAY(KBDD_DELAYTIME);
469             b = read_data(kbdc);
470             if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
471                 if ((b == PSM_ACK) || (b == PSM_RESEND) 
472                     || (b == PSM_RESET_FAIL))
473                     return b;
474                 addq(&kbdc->aux, b);
475             } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
476                 addq(&kbdc->kbd, b);
477             }
478         }
479         DELAY(KBDC_DELAYTIME);
480     }
481     return -1;
482 }
483
484 /*
485  * Returns read-back data or -1 on failure
486  */
487 int
488 write_controller_w1r1(KBDC p, int c, int d)
489 {
490     if (!write_controller_command(p, c))
491         return(-1);
492     if (!write_controller_data(p, d))
493         return(-1);
494     return (read_controller_data(p));
495 }
496
497 /* write a one byte command to the controller */
498 int
499 write_controller_command(KBDC p, int c)
500 {
501     if (!wait_while_controller_busy(kbdcp(p)))
502         return FALSE;
503     write_command(kbdcp(p), c);
504     return TRUE;
505 }
506
507 /* write a one byte data to the controller */
508 int
509 write_controller_data(KBDC p, int c)
510 {
511     if (!wait_while_controller_busy(kbdcp(p)))
512         return FALSE;
513     write_data(kbdcp(p), c);
514     return TRUE;
515 }
516
517 /* write a one byte keyboard command */
518 int
519 write_kbd_command(KBDC p, int c)
520 {
521     if (!wait_while_controller_busy(kbdcp(p)))
522         return FALSE;
523     write_data(kbdcp(p), c);
524     return TRUE;
525 }
526
527 /* write a one byte auxiliary device command */
528 int
529 write_aux_command(KBDC p, int c)
530 {
531     if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
532         return FALSE;
533     return write_controller_data(p, c);
534 }
535
536 /* send a command to the keyboard and wait for ACK */
537 int
538 send_kbd_command(KBDC p, int c)
539 {
540     int retry = KBD_MAXRETRY;
541     int res = -1;
542
543     while (retry-- > 0) {
544         if (!write_kbd_command(p, c))
545             continue;
546         res = wait_for_kbd_ack(kbdcp(p));
547         if (res == KBD_ACK)
548             break;
549     }
550     return res;
551 }
552
553 /* send a command to the auxiliary device and wait for ACK */
554 int
555 send_aux_command(KBDC p, int c)
556 {
557     int retry = KBD_MAXRETRY;
558     int res = -1;
559
560     while (retry-- > 0) {
561         if (!write_aux_command(p, c))
562             continue;
563         /*
564          * FIXME: XXX
565          * The aux device may have already sent one or two bytes of 
566          * status data, when a command is received. It will immediately 
567          * stop data transmission, thus, leaving an incomplete data 
568          * packet in our buffer. We have to discard any unprocessed
569          * data in order to remove such packets. Well, we may remove 
570          * unprocessed, but necessary data byte as well... 
571          */
572         emptyq(&kbdcp(p)->aux);
573         res = wait_for_aux_ack(kbdcp(p));
574         if (res == PSM_ACK)
575             break;
576     }
577     return res;
578 }
579
580 /* send a command and a data to the keyboard, wait for ACKs */
581 int
582 send_kbd_command_and_data(KBDC p, int c, int d)
583 {
584     int retry;
585     int res = -1;
586
587     for (retry = KBD_MAXRETRY; retry > 0; --retry) {
588         if (!write_kbd_command(p, c))
589             continue;
590         res = wait_for_kbd_ack(kbdcp(p));
591         if (res == KBD_ACK)
592             break;
593         else if (res != KBD_RESEND)
594             return res;
595     }
596     if (retry <= 0)
597         return res;
598
599     for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
600         if (!write_kbd_command(p, d))
601             continue;
602         res = wait_for_kbd_ack(kbdcp(p));
603         if (res != KBD_RESEND)
604             break;
605     }
606     return res;
607 }
608
609 /* send a command and a data to the auxiliary device, wait for ACKs */
610 int
611 send_aux_command_and_data(KBDC p, int c, int d)
612 {
613     int retry;
614     int res = -1;
615
616     for (retry = KBD_MAXRETRY; retry > 0; --retry) {
617         if (!write_aux_command(p, c))
618             continue;
619         emptyq(&kbdcp(p)->aux);
620         res = wait_for_aux_ack(kbdcp(p));
621         if (res == PSM_ACK)
622             break;
623         else if (res != PSM_RESEND)
624             return res;
625     }
626     if (retry <= 0)
627         return res;
628
629     for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
630         if (!write_aux_command(p, d))
631             continue;
632         res = wait_for_aux_ack(kbdcp(p));
633         if (res != PSM_RESEND)
634             break;
635     }
636     return res;
637 }
638
639 /* 
640  * read one byte from any source; whether from the controller,
641  * the keyboard, or the aux device
642  */
643 int
644 read_controller_data(KBDC p)
645 {
646     if (availq(&kbdcp(p)->kbd)) 
647         return removeq(&kbdcp(p)->kbd);
648     if (availq(&kbdcp(p)->aux)) 
649         return removeq(&kbdcp(p)->aux);
650     if (!wait_for_data(kbdcp(p)))
651         return -1;              /* timeout */
652     return read_data(kbdcp(p));
653 }
654
655 #if KBDIO_DEBUG >= 2
656 static int call = 0;
657 #endif
658
659 /* read one byte from the keyboard */
660 int
661 read_kbd_data(KBDC p)
662 {
663     unsigned char b;
664
665 #if KBDIO_DEBUG >= 2
666     if (++call > 2000) {
667         call = 0;
668         log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
669                              "aux q: %d calls, max %d chars\n",
670                        kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
671                        kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
672     }
673 #endif
674
675     if (availq(&kbdcp(p)->kbd)) 
676         return removeq(&kbdcp(p)->kbd);
677     if (!wait_for_kbd_data(kbdcp(p)))
678         return -1;              /* timeout */
679     b = read_data(kbdcp(p));
680     return b;
681 }
682
683 /* read one byte from the keyboard, but return immediately if 
684  * no data is waiting
685  */
686 int
687 read_kbd_data_no_wait(KBDC p)
688 {
689     int f;
690     unsigned char b;
691
692 #if KBDIO_DEBUG >= 2
693     if (++call > 2000) {
694         call = 0;
695         log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
696                              "aux q: %d calls, max %d chars\n",
697                        kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
698                        kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
699     }
700 #endif
701
702     if (availq(&kbdcp(p)->kbd)) 
703         return removeq(&kbdcp(p)->kbd);
704     f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
705     while (f == KBDS_AUX_BUFFER_FULL) {
706         DELAY(KBDD_DELAYTIME);
707         b = read_data(kbdcp(p));
708         addq(&kbdcp(p)->aux, b);
709         f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
710     }
711     if (f == KBDS_KBD_BUFFER_FULL) {
712         DELAY(KBDD_DELAYTIME);
713         b = read_data(kbdcp(p));
714         return (int)b;
715     }
716     return -1;          /* no data */
717 }
718
719 /* read one byte from the aux device */
720 int
721 read_aux_data(KBDC p)
722 {
723     unsigned char b;
724     if (availq(&kbdcp(p)->aux)) 
725         return removeq(&kbdcp(p)->aux);
726     if (!wait_for_aux_data(kbdcp(p)))
727         return -1;              /* timeout */
728     b = read_data(kbdcp(p));
729     return b;
730 }
731
732 /* read one byte from the aux device, but return immediately if 
733  * no data is waiting
734  */
735 int
736 read_aux_data_no_wait(KBDC p)
737 {
738     unsigned char b;
739     int f;
740
741     if (availq(&kbdcp(p)->aux)) 
742         return removeq(&kbdcp(p)->aux);
743     f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
744     while (f == KBDS_KBD_BUFFER_FULL) {
745         DELAY(KBDD_DELAYTIME);
746         b = read_data(kbdcp(p));
747         addq(&kbdcp(p)->kbd, b);
748         f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
749     }
750     if (f == KBDS_AUX_BUFFER_FULL) {
751         DELAY(KBDD_DELAYTIME);
752         b = read_data(kbdcp(p));
753         return b;
754     }
755     return -1;          /* no data */
756 }
757
758 /* discard data from the keyboard */
759 void
760 empty_kbd_buffer(KBDC p, int wait)
761 {
762     int t;
763     int b;
764     int f;
765 #if KBDIO_DEBUG >= 2
766     int c1 = 0;
767     int c2 = 0;
768 #endif
769     int delta = 2;
770
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_AUX_BUFFER_FULL) {
776                 addq(&kbdcp(p)->aux, b);
777 #if KBDIO_DEBUG >= 2
778                 ++c2;
779             } else {
780                 ++c1;
781 #endif
782             }
783             t = wait;
784         } else {
785             t -= delta;
786         }
787         DELAY(delta*1000);
788     }
789 #if KBDIO_DEBUG >= 2
790     if ((c1 > 0) || (c2 > 0))
791         log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
792 #endif
793
794     emptyq(&kbdcp(p)->kbd);
795 }
796
797 /* discard data from the aux device */
798 void
799 empty_aux_buffer(KBDC p, int wait)
800 {
801     int t;
802     int b;
803     int f;
804 #if KBDIO_DEBUG >= 2
805     int c1 = 0;
806     int c2 = 0;
807 #endif
808     int delta = 2;
809
810     for (t = wait; t > 0; ) { 
811         if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
812             DELAY(KBDD_DELAYTIME);
813             b = read_data(kbdcp(p));
814             if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
815                 addq(&kbdcp(p)->kbd, b);
816 #if KBDIO_DEBUG >= 2
817                 ++c1;
818             } else {
819                 ++c2;
820 #endif
821             }
822             t = wait;
823         } else {
824             t -= delta;
825         }
826         DELAY(delta*1000);
827     }
828 #if KBDIO_DEBUG >= 2
829     if ((c1 > 0) || (c2 > 0))
830         log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
831 #endif
832
833     emptyq(&kbdcp(p)->aux);
834 }
835
836 /* discard any data from the keyboard or the aux device */
837 void
838 empty_both_buffers(KBDC p, int wait)
839 {
840     int t;
841     int f;
842 #if KBDIO_DEBUG >= 2
843     int c1 = 0;
844     int c2 = 0;
845 #endif
846     int delta = 2;
847
848     for (t = wait; t > 0; ) { 
849         if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
850             DELAY(KBDD_DELAYTIME);
851             (void)read_data(kbdcp(p));
852 #if KBDIO_DEBUG >= 2
853             if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
854                 ++c1;
855             else
856                 ++c2;
857 #endif
858             t = wait;
859         } else {
860             t -= delta;
861         }
862         DELAY(delta*1000);
863     }
864 #if KBDIO_DEBUG >= 2
865     if ((c1 > 0) || (c2 > 0))
866         log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
867 #endif
868
869     emptyq(&kbdcp(p)->kbd);
870     emptyq(&kbdcp(p)->aux);
871 }
872
873 /* keyboard and mouse device control */
874
875 /* NOTE: enable the keyboard port but disable the keyboard 
876  * interrupt before calling "reset_kbd()".
877  */
878 int
879 reset_kbd(KBDC p)
880 {
881     int retry = KBD_MAXRETRY;
882     int again = KBD_MAXWAIT;
883     int c = KBD_RESEND;         /* keep the compiler happy */
884
885     while (retry-- > 0) {
886         empty_both_buffers(p, 10);
887         if (!write_kbd_command(p, KBDC_RESET_KBD))
888             continue;
889         emptyq(&kbdcp(p)->kbd);
890         c = read_controller_data(p);
891         if (verbose || bootverbose)
892             log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
893         if (c == KBD_ACK)       /* keyboard has agreed to reset itself... */
894             break;
895     }
896     if (retry < 0)
897         return FALSE;
898
899     while (again-- > 0) {
900         /* wait awhile, well, in fact we must wait quite loooooooooooong */
901         DELAY(KBD_RESETDELAY*1000);
902         c = read_controller_data(p);    /* RESET_DONE/RESET_FAIL */
903         if (c != -1)    /* wait again if the controller is not ready */
904             break;
905     }
906     if (verbose || bootverbose)
907         log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
908     if (c != KBD_RESET_DONE)
909         return FALSE;
910     return TRUE;
911 }
912
913 /* NOTE: enable the aux port but disable the aux interrupt
914  * before calling `reset_aux_dev()'.
915  */
916 int
917 reset_aux_dev(KBDC p)
918 {
919     int retry = KBD_MAXRETRY;
920     int again = KBD_MAXWAIT;
921     int c = PSM_RESEND;         /* keep the compiler happy */
922
923     while (retry-- > 0) {
924         empty_both_buffers(p, 10);
925         if (!write_aux_command(p, PSMC_RESET_DEV))
926             continue;
927         emptyq(&kbdcp(p)->aux);
928         /* NOTE: Compaq Armada laptops require extra delay here. XXX */
929         for (again = KBD_MAXWAIT; again > 0; --again) {
930             DELAY(KBD_RESETDELAY*1000);
931             c = read_aux_data_no_wait(p);
932             if (c != -1)
933                 break;
934         }
935         if (verbose || bootverbose)
936             log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
937         if (c == PSM_ACK)       /* aux dev is about to reset... */
938             break;
939     }
940     if (retry < 0)
941         return FALSE;
942
943     for (again = KBD_MAXWAIT; again > 0; --again) {
944         /* wait awhile, well, quite looooooooooooong */
945         DELAY(KBD_RESETDELAY*1000);
946         c = read_aux_data_no_wait(p);   /* RESET_DONE/RESET_FAIL */
947         if (c != -1)    /* wait again if the controller is not ready */
948             break;
949     }
950     if (verbose || bootverbose)
951         log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
952     if (c != PSM_RESET_DONE)    /* reset status */
953         return FALSE;
954
955     c = read_aux_data(p);       /* device ID */
956     if (verbose || bootverbose)
957         log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
958     /* NOTE: we could check the device ID now, but leave it later... */
959     return TRUE;
960 }
961
962 /* controller diagnostics and setup */
963
964 int
965 test_controller(KBDC p)
966 {
967     int retry = KBD_MAXRETRY;
968     int again = KBD_MAXWAIT;
969     int c = KBD_DIAG_FAIL;
970
971     while (retry-- > 0) {
972         empty_both_buffers(p, 10);
973         if (write_controller_command(p, KBDC_DIAGNOSE))
974             break;
975     }
976     if (retry < 0)
977         return FALSE;
978
979     emptyq(&kbdcp(p)->kbd);
980     while (again-- > 0) {
981         /* wait awhile */
982         DELAY(KBD_RESETDELAY*1000);
983         c = read_controller_data(p);    /* DIAG_DONE/DIAG_FAIL */
984         if (c != -1)    /* wait again if the controller is not ready */
985             break;
986     }
987     if (verbose || bootverbose)
988         log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
989     return (c == KBD_DIAG_DONE);
990 }
991
992 int
993 test_kbd_port(KBDC p)
994 {
995     int retry = KBD_MAXRETRY;
996     int again = KBD_MAXWAIT;
997     int c = -1;
998
999     while (retry-- > 0) {
1000         empty_both_buffers(p, 10);
1001         if (write_controller_command(p, KBDC_TEST_KBD_PORT))
1002             break;
1003     }
1004     if (retry < 0)
1005         return FALSE;
1006
1007     emptyq(&kbdcp(p)->kbd);
1008     while (again-- > 0) {
1009         c = read_controller_data(p);
1010         if (c != -1)    /* try again if the controller is not ready */
1011             break;
1012     }
1013     if (verbose || bootverbose)
1014         log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
1015     return c;
1016 }
1017
1018 int
1019 test_aux_port(KBDC p)
1020 {
1021     int retry = KBD_MAXRETRY;
1022     int again = KBD_MAXWAIT;
1023     int c = -1;
1024
1025     while (retry-- > 0) {
1026         empty_both_buffers(p, 10);
1027         if (write_controller_command(p, KBDC_TEST_AUX_PORT))
1028             break;
1029     }
1030     if (retry < 0)
1031         return FALSE;
1032
1033     emptyq(&kbdcp(p)->kbd);
1034     while (again-- > 0) {
1035         c = read_controller_data(p);
1036         if (c != -1)    /* try again if the controller is not ready */
1037             break;
1038     }
1039     if (verbose || bootverbose)
1040         log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1041     return c;
1042 }
1043
1044 int
1045 get_controller_command_byte(KBDC p)
1046 {
1047     if (kbdcp(p)->command_byte != -1)
1048         return kbdcp(p)->command_byte;
1049     if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1050         return -1;
1051     emptyq(&kbdcp(p)->kbd);
1052     kbdcp(p)->command_byte = read_controller_data(p);
1053     return kbdcp(p)->command_byte;
1054 }
1055
1056 int
1057 set_controller_command_byte(KBDC p, int mask, int command)
1058 {
1059     if (get_controller_command_byte(p) == -1)
1060         return FALSE;
1061
1062     command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1063 #if 0
1064     if (mask & KBD_DISABLE_KBD_PORT) {
1065             if (command & KBD_DISABLE_KBD_PORT) {
1066                 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1067                     return FALSE;
1068             }
1069     }
1070 #endif
1071     if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1072         return FALSE;
1073     if (!write_controller_data(p, command))
1074         return FALSE;
1075 #if 0
1076     if (mask & KBD_DISABLE_KBD_PORT) {
1077             if ((command & KBD_DISABLE_KBD_PORT) == 0) {
1078                 if (!write_controller_command(p, KBDC_ENABLE_KBD_PORT))
1079                     return FALSE;
1080             }
1081     }
1082 #endif
1083     kbdcp(p)->command_byte = command;
1084
1085     if (verbose)
1086         log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",
1087             command);
1088
1089     return TRUE;
1090 }