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