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