Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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.11 2007/04/22 10:54:43 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 #if 0
52 #define lwkt_gettoken(x)
53 #define lwkt_reltoken(x)
54 #endif
55
56 /* constants */
57
58 #define MAXKBDC         MAX(NATKBDC, 1)         /* XXX */
59
60 /* macros */
61
62 #ifndef MAX
63 #define MAX(x, y)       ((x) > (y) ? (x) : (y))
64 #endif
65
66 #define kbdcp(p)        ((atkbdc_softc_t *)(p))
67 #define nextq(i)        (((i) + 1) % KBDQ_BUFSIZE)
68 #define availq(q)       ((q)->head != (q)->tail)
69 #if KBDIO_DEBUG >= 2
70 #define emptyq(q)       ((q)->tail = (q)->head = (q)->qcount = 0)
71 #else
72 #define emptyq(q)       ((q)->tail = (q)->head = 0)
73 #endif
74
75 #define read_data(k)    (bus_space_read_1((k)->iot, (k)->ioh0, 0))
76 #define read_status(k)  (bus_space_read_1((k)->iot, (k)->ioh1, 0))
77 #define write_data(k, d)        \
78                         (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d)))
79 #define write_command(k, d)     \
80                         (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d)))
81
82 /* local variables */
83
84 /*
85  * We always need at least one copy of the kbdc_softc struct for the
86  * low-level console.  As the low-level console accesses the keyboard
87  * controller before kbdc, and all other devices, is probed, we
88  * statically allocate one entry. XXX
89  */
90 static atkbdc_softc_t default_kbdc;
91 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
92
93 static int verbose = KBDIO_DEBUG;
94
95 /* function prototypes */
96
97 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag,
98                         bus_space_handle_t h0, bus_space_handle_t h1);
99 static int addq(kbdkqueue *q, int c);
100 static int removeq(kbdkqueue *q);
101 static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
102 static int wait_for_data(atkbdc_softc_t *kbdc);
103 static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
104 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
105 static int wait_for_aux_data(atkbdc_softc_t *kbdc);
106 static int wait_for_aux_ack(atkbdc_softc_t *kbdc);
107
108 atkbdc_softc_t *
109 atkbdc_get_softc(int unit)
110 {
111         atkbdc_softc_t *sc;
112
113         if (unit >= sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]))
114                 return NULL;
115         sc = atkbdc_softc[unit];
116         if (sc == NULL) {
117                 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
118                 atkbdc_softc[unit] = sc;
119         }
120         return sc;
121 }
122
123 int
124 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1)
125 {
126         if (rman_get_start(port0) <= 0)
127                 return ENXIO;
128         if (rman_get_start(port1) <= 0)
129                 return ENXIO;
130         return 0;
131 }
132
133 int
134 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0,
135                    struct resource *port1)
136 {
137         return atkbdc_setup(sc, rman_get_bustag(port0),
138                             rman_get_bushandle(port0),
139                             rman_get_bushandle(port1));
140 }
141
142 /* the backdoor to the keyboard controller! XXX */
143 int
144 atkbdc_configure(void)
145 {
146         bus_space_tag_t tag;
147         bus_space_handle_t h0;
148         bus_space_handle_t h1;
149         int port0;
150         int port1;
151 #if defined(__i386__)
152         volatile int i;
153 #endif
154
155         port0 = IO_KBD;
156         resource_int_value("atkbdc", 0, "port", &port0);
157         port1 = IO_KBD + KBD_STATUS_PORT;
158 #if 0
159         resource_int_value("atkbdc", 0, "port", &port0);
160 #endif
161
162         /* XXX: tag should be passed from the caller */
163 #if defined(__i386__)
164         tag = I386_BUS_SPACE_IO;
165 #else
166         tag = 0;        /* XXX */
167 #endif
168
169 #if notyet
170         bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0);
171         bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1);
172 #else
173         h0 = (bus_space_handle_t)port0;
174         h1 = (bus_space_handle_t)port1;
175 #endif
176
177 #if defined(__i386__)
178         /*
179          * Check if we really have AT keyboard controller. Poll status
180          * register until we get "all clear" indication. If no such
181          * indication comes, it probably means that there is no AT
182          * keyboard controller present. Give up in such case. Check relies
183          * on the fact that reading from non-existing in/out port returns
184          * 0xff on i386. May or may not be true on other platforms.
185          */
186         for (i = 65536; i != 0; --i) {
187                 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0)
188                         break;
189                 DELAY(16);
190         }
191         if (i == 0)
192                 return ENXIO;
193 #endif
194
195         return atkbdc_setup(atkbdc_softc[0], tag, h0, h1);
196 }
197
198 static int
199 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0,
200              bus_space_handle_t h1)
201 {
202         if (sc->ioh0 == 0) {    /* XXX */
203             sc->command_byte = -1;
204             sc->command_mask = 0;
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     int retry = 5000;
336     int f;
337
338     while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) {
339         if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
340             DELAY(KBDD_DELAYTIME);
341             addq(&kbdc->kbd, read_data(kbdc));
342         } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
343             DELAY(KBDD_DELAYTIME);
344             addq(&kbdc->aux, read_data(kbdc));
345         }
346         DELAY(KBDC_DELAYTIME);
347         if (--retry < 0)
348             return FALSE;
349     }
350     return TRUE;
351 }
352
353 /*
354  * wait for any data; whether it's from the controller, 
355  * the keyboard, or the aux device.
356  */
357 static int
358 wait_for_data(struct atkbdc_softc *kbdc)
359 {
360     /* CPU will stay inside the loop for 200msec at most */
361     int retry = 10000;
362     int f;
363
364     while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) {
365         DELAY(KBDC_DELAYTIME);
366         if (--retry < 0)
367             return 0;
368     }
369     DELAY(KBDD_DELAYTIME);
370     return f;
371 }
372
373 /* wait for data from the keyboard */
374 static int
375 wait_for_kbd_data(struct atkbdc_softc *kbdc)
376 {
377     /* CPU will stay inside the loop for 200msec at most */
378     int retry = 10000;
379     int f;
380
381     while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
382             != KBDS_KBD_BUFFER_FULL) {
383         if (f == KBDS_AUX_BUFFER_FULL) {
384             DELAY(KBDD_DELAYTIME);
385             addq(&kbdc->aux, read_data(kbdc));
386         }
387         DELAY(KBDC_DELAYTIME);
388         if (--retry < 0)
389             return 0;
390     }
391     DELAY(KBDD_DELAYTIME);
392     return f;
393 }
394
395 /* 
396  * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
397  * queue anything else.
398  */
399 static int
400 wait_for_kbd_ack(struct atkbdc_softc *kbdc)
401 {
402     /* CPU will stay inside the loop for 200msec at most */
403     int retry = 10000;
404     int f;
405     int b;
406
407     while (retry-- > 0) {
408         if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
409             DELAY(KBDD_DELAYTIME);
410             b = read_data(kbdc);
411             if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
412                 if ((b == KBD_ACK) || (b == KBD_RESEND) 
413                     || (b == KBD_RESET_FAIL))
414                     return b;
415                 addq(&kbdc->kbd, b);
416             } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
417                 addq(&kbdc->aux, b);
418             }
419         }
420         DELAY(KBDC_DELAYTIME);
421     }
422     return -1;
423 }
424
425 /* wait for data from the aux device */
426 static int
427 wait_for_aux_data(struct atkbdc_softc *kbdc)
428 {
429     /* CPU will stay inside the loop for 200msec at most */
430     int retry = 10000;
431     int f;
432
433     while ((f = read_status(kbdc) & KBDS_BUFFER_FULL)
434             != KBDS_AUX_BUFFER_FULL) {
435         if (f == KBDS_KBD_BUFFER_FULL) {
436             DELAY(KBDD_DELAYTIME);
437             addq(&kbdc->kbd, read_data(kbdc));
438         }
439         DELAY(KBDC_DELAYTIME);
440         if (--retry < 0)
441             return 0;
442     }
443     DELAY(KBDD_DELAYTIME);
444     return f;
445 }
446
447 /* 
448  * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
449  * queue anything else.
450  */
451 static int
452 wait_for_aux_ack(struct atkbdc_softc *kbdc)
453 {
454     /* CPU will stay inside the loop for 200msec at most */
455     int retry = 10000;
456     int f;
457     int b;
458
459     while (retry-- > 0) {
460         if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) {
461             DELAY(KBDD_DELAYTIME);
462             b = read_data(kbdc);
463             if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
464                 if ((b == PSM_ACK) || (b == PSM_RESEND) 
465                     || (b == PSM_RESET_FAIL))
466                     return b;
467                 addq(&kbdc->aux, b);
468             } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
469                 addq(&kbdc->kbd, b);
470             }
471         }
472         DELAY(KBDC_DELAYTIME);
473     }
474     return -1;
475 }
476
477 /* write a one byte command to the controller */
478 int
479 write_controller_command(KBDC p, int c)
480 {
481     if (!wait_while_controller_busy(kbdcp(p)))
482         return FALSE;
483     write_command(kbdcp(p), c);
484     return TRUE;
485 }
486
487 /* write a one byte data to the controller */
488 int
489 write_controller_data(KBDC p, int c)
490 {
491     if (!wait_while_controller_busy(kbdcp(p)))
492         return FALSE;
493     write_data(kbdcp(p), c);
494     return TRUE;
495 }
496
497 /* write a one byte keyboard command */
498 int
499 write_kbd_command(KBDC p, int c)
500 {
501     if (!wait_while_controller_busy(kbdcp(p)))
502         return FALSE;
503     write_data(kbdcp(p), c);
504     return TRUE;
505 }
506
507 /* write a one byte auxiliary device command */
508 int
509 write_aux_command(KBDC p, int c)
510 {
511     if (!write_controller_command(p, KBDC_WRITE_TO_AUX))
512         return FALSE;
513     return write_controller_data(p, c);
514 }
515
516 /* send a command to the keyboard and wait for ACK */
517 int
518 send_kbd_command(KBDC p, int c)
519 {
520     int retry = KBD_MAXRETRY;
521     int res = -1;
522
523     while (retry-- > 0) {
524         if (!write_kbd_command(p, c))
525             continue;
526         res = wait_for_kbd_ack(kbdcp(p));
527         if (res == KBD_ACK)
528             break;
529     }
530     return res;
531 }
532
533 /* send a command to the auxiliary device and wait for ACK */
534 int
535 send_aux_command(KBDC p, int c)
536 {
537     int retry = KBD_MAXRETRY;
538     int res = -1;
539
540     while (retry-- > 0) {
541         if (!write_aux_command(p, c))
542             continue;
543         /*
544          * FIXME: XXX
545          * The aux device may have already sent one or two bytes of 
546          * status data, when a command is received. It will immediately 
547          * stop data transmission, thus, leaving an incomplete data 
548          * packet in our buffer. We have to discard any unprocessed
549          * data in order to remove such packets. Well, we may remove 
550          * unprocessed, but necessary data byte as well... 
551          */
552         emptyq(&kbdcp(p)->aux);
553         res = wait_for_aux_ack(kbdcp(p));
554         if (res == PSM_ACK)
555             break;
556     }
557     return res;
558 }
559
560 /* send a command and a data to the keyboard, wait for ACKs */
561 int
562 send_kbd_command_and_data(KBDC p, int c, int d)
563 {
564     int retry;
565     int res = -1;
566
567     for (retry = KBD_MAXRETRY; retry > 0; --retry) {
568         if (!write_kbd_command(p, c))
569             continue;
570         res = wait_for_kbd_ack(kbdcp(p));
571         if (res == KBD_ACK)
572             break;
573         else if (res != KBD_RESEND)
574             return res;
575     }
576     if (retry <= 0)
577         return res;
578
579     for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
580         if (!write_kbd_command(p, d))
581             continue;
582         res = wait_for_kbd_ack(kbdcp(p));
583         if (res != KBD_RESEND)
584             break;
585     }
586     return res;
587 }
588
589 /* send a command and a data to the auxiliary device, wait for ACKs */
590 int
591 send_aux_command_and_data(KBDC p, int c, int d)
592 {
593     int retry;
594     int res = -1;
595
596     for (retry = KBD_MAXRETRY; retry > 0; --retry) {
597         if (!write_aux_command(p, c))
598             continue;
599         emptyq(&kbdcp(p)->aux);
600         res = wait_for_aux_ack(kbdcp(p));
601         if (res == PSM_ACK)
602             break;
603         else if (res != PSM_RESEND)
604             return res;
605     }
606     if (retry <= 0)
607         return res;
608
609     for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
610         if (!write_aux_command(p, d))
611             continue;
612         res = wait_for_aux_ack(kbdcp(p));
613         if (res != PSM_RESEND)
614             break;
615     }
616     return res;
617 }
618
619 /* 
620  * read one byte from any source; whether from the controller,
621  * the keyboard, or the aux device
622  */
623 int
624 read_controller_data(KBDC p)
625 {
626     if (availq(&kbdcp(p)->kbd)) 
627         return removeq(&kbdcp(p)->kbd);
628     if (availq(&kbdcp(p)->aux)) 
629         return removeq(&kbdcp(p)->aux);
630     if (!wait_for_data(kbdcp(p)))
631         return -1;              /* timeout */
632     return read_data(kbdcp(p));
633 }
634
635 #if KBDIO_DEBUG >= 2
636 static int call = 0;
637 #endif
638
639 /* read one byte from the keyboard */
640 int
641 read_kbd_data(KBDC p)
642 {
643 #if KBDIO_DEBUG >= 2
644     if (++call > 2000) {
645         call = 0;
646         log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
647                              "aux q: %d calls, max %d chars\n",
648                        kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
649                        kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
650     }
651 #endif
652
653     if (availq(&kbdcp(p)->kbd)) 
654         return removeq(&kbdcp(p)->kbd);
655     if (!wait_for_kbd_data(kbdcp(p)))
656         return -1;              /* timeout */
657     return read_data(kbdcp(p));
658 }
659
660 /* read one byte from the keyboard, but return immediately if 
661  * no data is waiting
662  */
663 int
664 read_kbd_data_no_wait(KBDC p)
665 {
666     int f;
667
668 #if KBDIO_DEBUG >= 2
669     if (++call > 2000) {
670         call = 0;
671         log(LOG_DEBUG, "kbdc: kbd q: %d calls, max %d chars, "
672                              "aux q: %d calls, max %d chars\n",
673                        kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
674                        kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
675     }
676 #endif
677
678     if (availq(&kbdcp(p)->kbd)) 
679         return removeq(&kbdcp(p)->kbd);
680     f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
681     if (f == KBDS_AUX_BUFFER_FULL) {
682         DELAY(KBDD_DELAYTIME);
683         addq(&kbdcp(p)->aux, read_data(kbdcp(p)));
684         f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
685     }
686     if (f == KBDS_KBD_BUFFER_FULL) {
687         DELAY(KBDD_DELAYTIME);
688         return read_data(kbdcp(p));
689     }
690     return -1;          /* no data */
691 }
692
693 /* read one byte from the aux device */
694 int
695 read_aux_data(KBDC p)
696 {
697     if (availq(&kbdcp(p)->aux)) 
698         return removeq(&kbdcp(p)->aux);
699     if (!wait_for_aux_data(kbdcp(p)))
700         return -1;              /* timeout */
701     return read_data(kbdcp(p));
702 }
703
704 /* read one byte from the aux device, but return immediately if 
705  * no data is waiting
706  */
707 int
708 read_aux_data_no_wait(KBDC p)
709 {
710     int f;
711
712     if (availq(&kbdcp(p)->aux)) 
713         return removeq(&kbdcp(p)->aux);
714     f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
715     if (f == KBDS_KBD_BUFFER_FULL) {
716         DELAY(KBDD_DELAYTIME);
717         addq(&kbdcp(p)->kbd, read_data(kbdcp(p)));
718         f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL;
719     }
720     if (f == KBDS_AUX_BUFFER_FULL) {
721         DELAY(KBDD_DELAYTIME);
722         return read_data(kbdcp(p));
723     }
724     return -1;          /* no data */
725 }
726
727 /* discard data from the keyboard */
728 void
729 empty_kbd_buffer(KBDC p, int wait)
730 {
731     int t;
732     int b;
733     int f;
734 #if KBDIO_DEBUG >= 2
735     int c1 = 0;
736     int c2 = 0;
737 #endif
738     int delta = 2;
739
740     for (t = wait; t > 0; ) { 
741         if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
742             DELAY(KBDD_DELAYTIME);
743             b = read_data(kbdcp(p));
744             if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
745                 addq(&kbdcp(p)->aux, b);
746 #if KBDIO_DEBUG >= 2
747                 ++c2;
748             } else {
749                 ++c1;
750 #endif
751             }
752             t = wait;
753         } else {
754             t -= delta;
755         }
756         DELAY(delta*1000);
757     }
758 #if KBDIO_DEBUG >= 2
759     if ((c1 > 0) || (c2 > 0))
760         log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, c2);
761 #endif
762
763     emptyq(&kbdcp(p)->kbd);
764 }
765
766 /* discard data from the aux device */
767 void
768 empty_aux_buffer(KBDC p, int wait)
769 {
770     int t;
771     int b;
772     int f;
773 #if KBDIO_DEBUG >= 2
774     int c1 = 0;
775     int c2 = 0;
776 #endif
777     int delta = 2;
778
779     for (t = wait; t > 0; ) { 
780         if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
781             DELAY(KBDD_DELAYTIME);
782             b = read_data(kbdcp(p));
783             if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
784                 addq(&kbdcp(p)->kbd, b);
785 #if KBDIO_DEBUG >= 2
786                 ++c1;
787             } else {
788                 ++c2;
789 #endif
790             }
791             t = wait;
792         } else {
793             t -= delta;
794         }
795         DELAY(delta*1000);
796     }
797 #if KBDIO_DEBUG >= 2
798     if ((c1 > 0) || (c2 > 0))
799         log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, c2);
800 #endif
801
802     emptyq(&kbdcp(p)->aux);
803 }
804
805 /* discard any data from the keyboard or the aux device */
806 void
807 empty_both_buffers(KBDC p, int wait)
808 {
809     int t;
810     int f;
811 #if KBDIO_DEBUG >= 2
812     int c1 = 0;
813     int c2 = 0;
814 #endif
815     int delta = 2;
816
817     for (t = wait; t > 0; ) { 
818         if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) {
819             DELAY(KBDD_DELAYTIME);
820             (void)read_data(kbdcp(p));
821 #if KBDIO_DEBUG >= 2
822             if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
823                 ++c1;
824             else
825                 ++c2;
826 #endif
827             t = wait;
828         } else {
829             t -= delta;
830         }
831         DELAY(delta*1000);
832     }
833 #if KBDIO_DEBUG >= 2
834     if ((c1 > 0) || (c2 > 0))
835         log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", c1, c2);
836 #endif
837
838     emptyq(&kbdcp(p)->kbd);
839     emptyq(&kbdcp(p)->aux);
840 }
841
842 /* keyboard and mouse device control */
843
844 /* NOTE: enable the keyboard port but disable the keyboard 
845  * interrupt before calling "reset_kbd()".
846  */
847 int
848 reset_kbd(KBDC p)
849 {
850     int retry = KBD_MAXRETRY;
851     int again = KBD_MAXWAIT;
852     int c = KBD_RESEND;         /* keep the compiler happy */
853
854     while (retry-- > 0) {
855         empty_both_buffers(p, 10);
856         if (!write_kbd_command(p, KBDC_RESET_KBD))
857             continue;
858         emptyq(&kbdcp(p)->kbd);
859         c = read_controller_data(p);
860         if (verbose || bootverbose)
861             log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c);
862         if (c == KBD_ACK)       /* keyboard has agreed to reset itself... */
863             break;
864     }
865     if (retry < 0)
866         return FALSE;
867
868     while (again-- > 0) {
869         /* wait awhile, well, in fact we must wait quite loooooooooooong */
870         DELAY(KBD_RESETDELAY*1000);
871         c = read_controller_data(p);    /* RESET_DONE/RESET_FAIL */
872         if (c != -1)    /* wait again if the controller is not ready */
873             break;
874     }
875     if (verbose || bootverbose)
876         log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c);
877     if (c != KBD_RESET_DONE)
878         return FALSE;
879     return TRUE;
880 }
881
882 /* NOTE: enable the aux port but disable the aux interrupt
883  * before calling `reset_aux_dev()'.
884  */
885 int
886 reset_aux_dev(KBDC p)
887 {
888     int retry = KBD_MAXRETRY;
889     int again = KBD_MAXWAIT;
890     int c = PSM_RESEND;         /* keep the compiler happy */
891
892     while (retry-- > 0) {
893         empty_both_buffers(p, 10);
894         if (!write_aux_command(p, PSMC_RESET_DEV))
895             continue;
896         emptyq(&kbdcp(p)->aux);
897         /* NOTE: Compaq Armada laptops require extra delay here. XXX */
898         for (again = KBD_MAXWAIT; again > 0; --again) {
899             DELAY(KBD_RESETDELAY*1000);
900             c = read_aux_data_no_wait(p);
901             if (c != -1)
902                 break;
903         }
904         if (verbose || bootverbose)
905             log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c);
906         if (c == PSM_ACK)       /* aux dev is about to reset... */
907             break;
908     }
909     if (retry < 0)
910         return FALSE;
911
912     for (again = KBD_MAXWAIT; again > 0; --again) {
913         /* wait awhile, well, quite looooooooooooong */
914         DELAY(KBD_RESETDELAY*1000);
915         c = read_aux_data_no_wait(p);   /* RESET_DONE/RESET_FAIL */
916         if (c != -1)    /* wait again if the controller is not ready */
917             break;
918     }
919     if (verbose || bootverbose)
920         log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c);
921     if (c != PSM_RESET_DONE)    /* reset status */
922         return FALSE;
923
924     c = read_aux_data(p);       /* device ID */
925     if (verbose || bootverbose)
926         log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c);
927     /* NOTE: we could check the device ID now, but leave it later... */
928     return TRUE;
929 }
930
931 /* controller diagnostics and setup */
932
933 int
934 test_controller(KBDC p)
935 {
936     int retry = KBD_MAXRETRY;
937     int again = KBD_MAXWAIT;
938     int c = KBD_DIAG_FAIL;
939
940     while (retry-- > 0) {
941         empty_both_buffers(p, 10);
942         if (write_controller_command(p, KBDC_DIAGNOSE))
943             break;
944     }
945     if (retry < 0)
946         return FALSE;
947
948     emptyq(&kbdcp(p)->kbd);
949     while (again-- > 0) {
950         /* wait awhile */
951         DELAY(KBD_RESETDELAY*1000);
952         c = read_controller_data(p);    /* DIAG_DONE/DIAG_FAIL */
953         if (c != -1)    /* wait again if the controller is not ready */
954             break;
955     }
956     if (verbose || bootverbose)
957         log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c);
958     return (c == KBD_DIAG_DONE);
959 }
960
961 int
962 test_kbd_port(KBDC p)
963 {
964     int retry = KBD_MAXRETRY;
965     int again = KBD_MAXWAIT;
966     int c = -1;
967
968     while (retry-- > 0) {
969         empty_both_buffers(p, 10);
970         if (write_controller_command(p, KBDC_TEST_KBD_PORT))
971             break;
972     }
973     if (retry < 0)
974         return FALSE;
975
976     emptyq(&kbdcp(p)->kbd);
977     while (again-- > 0) {
978         c = read_controller_data(p);
979         if (c != -1)    /* try again if the controller is not ready */
980             break;
981     }
982     if (verbose || bootverbose)
983         log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c);
984     return c;
985 }
986
987 int
988 test_aux_port(KBDC p)
989 {
990     int retry = KBD_MAXRETRY;
991     int again = KBD_MAXWAIT;
992     int c = -1;
993
994     while (retry-- > 0) {
995         empty_both_buffers(p, 10);
996         if (write_controller_command(p, KBDC_TEST_AUX_PORT))
997             break;
998     }
999     if (retry < 0)
1000         return FALSE;
1001
1002     emptyq(&kbdcp(p)->kbd);
1003     while (again-- > 0) {
1004         c = read_controller_data(p);
1005         if (c != -1)    /* try again if the controller is not ready */
1006             break;
1007     }
1008     if (verbose || bootverbose)
1009         log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c);
1010     return c;
1011 }
1012
1013 int
1014 kbdc_get_device_mask(KBDC p)
1015 {
1016     return kbdcp(p)->command_mask;
1017 }
1018
1019 void
1020 kbdc_set_device_mask(KBDC p, int mask)
1021 {
1022     kbdcp(p)->command_mask = 
1023         mask & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS);
1024 }
1025
1026 int
1027 get_controller_command_byte(KBDC p)
1028 {
1029     if (kbdcp(p)->command_byte != -1)
1030         return kbdcp(p)->command_byte;
1031     if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE))
1032         return -1;
1033     emptyq(&kbdcp(p)->kbd);
1034     kbdcp(p)->command_byte = read_controller_data(p);
1035     return kbdcp(p)->command_byte;
1036 }
1037
1038 int
1039 set_controller_command_byte(KBDC p, int mask, int command)
1040 {
1041     if (get_controller_command_byte(p) == -1)
1042         return FALSE;
1043
1044     command = (kbdcp(p)->command_byte & ~mask) | (command & mask);
1045     if (command & KBD_DISABLE_KBD_PORT) {
1046         if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT))
1047             return FALSE;
1048     }
1049     if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE))
1050         return FALSE;
1051     if (!write_controller_data(p, command))
1052         return FALSE;
1053     kbdcp(p)->command_byte = command;
1054
1055     if (verbose)
1056         log(LOG_DEBUG, "kbdc: new command byte:%04x (set_controller...)\n",
1057             command);
1058
1059     return TRUE;
1060 }