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