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