Deleted Added
sdiff udiff text old ( 56836 ) new ( 58271 )
full compact
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:

--- 13 unchanged lines hidden (view full) ---

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: head/sys/dev/atkbdc/atkbdc.c 56836 2000-01-29 15:08:56Z peter $
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#include <isa/isareg.h>
48
49/* constants */

--- 10 unchanged lines hidden (view full) ---

60#define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
61#define availq(q) ((q)->head != (q)->tail)
62#if KBDIO_DEBUG >= 2
63#define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
64#else
65#define emptyq(q) ((q)->tail = (q)->head = 0)
66#endif
67
68/* local variables */
69
70/*
71 * We always need at least one copy of the kbdc_softc struct for the
72 * low-level console. As the low-level console accesses the keyboard
73 * controller before kbdc, and all other devices, is probed, we
74 * statically allocate one entry. XXX
75 */
76static atkbdc_softc_t default_kbdc;
77static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc };
78
79static int verbose = KBDIO_DEBUG;
80
81/* function prototypes */
82
83static int atkbdc_setup(atkbdc_softc_t *sc, int port);
84static int addq(kqueue *q, int c);
85static int removeq(kqueue *q);
86static int wait_while_controller_busy(atkbdc_softc_t *kbdc);
87static int wait_for_data(atkbdc_softc_t *kbdc);
88static int wait_for_kbd_data(atkbdc_softc_t *kbdc);
89static int wait_for_kbd_ack(atkbdc_softc_t *kbdc);
90static int wait_for_aux_data(atkbdc_softc_t *kbdc);
91static int wait_for_aux_ack(atkbdc_softc_t *kbdc);

--- 7 unchanged lines hidden (view full) ---

99 return NULL;
100 sc = atkbdc_softc[unit];
101 if (sc == NULL) {
102 sc = atkbdc_softc[unit]
103 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
104 if (sc == NULL)
105 return NULL;
106 bzero(sc, sizeof(*sc));
107 sc->port = -1; /* XXX */
108 }
109 return sc;
110}
111
112int
113atkbdc_probe_unit(int unit, int port)
114{
115 if (port <= 0)
116 return ENXIO;
117 return 0;
118}
119
120int
121atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, int port)
122{
123 return atkbdc_setup(sc, port);
124}
125
126/* the backdoor to the keyboard controller! XXX */
127int
128atkbdc_configure(void)
129{
130 return atkbdc_setup(atkbdc_softc[0], -1);
131}
132
133static int
134atkbdc_setup(atkbdc_softc_t *sc, int port)
135{
136 if (port <= 0)
137 port = IO_KBD;
138
139 if (sc->port <= 0) {
140 sc->command_byte = -1;
141 sc->command_mask = 0;
142 sc->lock = FALSE;
143 sc->kbd.head = sc->kbd.tail = 0;
144 sc->aux.head = sc->aux.tail = 0;
145#if KBDIO_DEBUG >= 2
146 sc->kbd.call_count = 0;
147 sc->kbd.qcount = sc->kbd.max_qcount = 0;
148 sc->aux.call_count = 0;
149 sc->aux.qcount = sc->aux.max_qcount = 0;
150#endif
151 }
152 sc->port = port; /* may override the previous value */
153 return 0;
154}
155
156/* associate a port number with a KBDC */
157
158KBDC
159kbdc_open(int port)
160{
161 int s;
162 int i;
163
164 if (port <= 0)
165 port = IO_KBD;
166
167 s = spltty();
168 for (i = 0; i < sizeof(atkbdc_softc)/sizeof(atkbdc_softc[0]); ++i) {
169 if (atkbdc_softc[i] == NULL)
170 continue;
171 if (atkbdc_softc[i]->port == port) {
172 splx(s);
173 return (KBDC)atkbdc_softc[i];
174 }
175 if (atkbdc_softc[i]->port <= 0) {
176 if (atkbdc_setup(atkbdc_softc[i], port))
177 break;
178 splx(s);
179 return (KBDC)atkbdc_softc[i];
180 }
181 }
182 splx(s);
183 return NULL;
184}
185
186/*
187 * I/O access arbitration in `kbdio'
188 *
189 * The `kbdio' module uses a simplistic convention to arbitrate
190 * I/O access to the controller/keyboard/mouse. The convention requires

--- 42 unchanged lines hidden (view full) ---

233 return (prevlock != lock);
234}
235
236/* check if any data is waiting to be processed */
237int
238kbdc_data_ready(KBDC p)
239{
240 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux)
241 || (inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL));
242}
243
244/* queuing functions */
245
246static int
247addq(kqueue *q, int c)
248{
249 if (nextq(q->tail) != q->head) {

--- 29 unchanged lines hidden (view full) ---

279/*
280 * device I/O routines
281 */
282static int
283wait_while_controller_busy(struct atkbdc_softc *kbdc)
284{
285 /* CPU will stay inside the loop for 100msec at most */
286 int retry = 5000;
287 int port = kbdc->port;
288 int f;
289
290 while ((f = inb(port + KBD_STATUS_PORT)) & KBDS_INPUT_BUFFER_FULL) {
291 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
292 DELAY(KBDD_DELAYTIME);
293 addq(&kbdc->kbd, inb(port + KBD_DATA_PORT));
294 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
295 DELAY(KBDD_DELAYTIME);
296 addq(&kbdc->aux, inb(port + KBD_DATA_PORT));
297 }
298 DELAY(KBDC_DELAYTIME);
299 if (--retry < 0)
300 return FALSE;
301 }
302 return TRUE;
303}
304
305/*
306 * wait for any data; whether it's from the controller,
307 * the keyboard, or the aux device.
308 */
309static int
310wait_for_data(struct atkbdc_softc *kbdc)
311{
312 /* CPU will stay inside the loop for 200msec at most */
313 int retry = 10000;
314 int port = kbdc->port;
315 int f;
316
317 while ((f = inb(port + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) == 0) {
318 DELAY(KBDC_DELAYTIME);
319 if (--retry < 0)
320 return 0;
321 }
322 DELAY(KBDD_DELAYTIME);
323 return f;
324}
325
326/* wait for data from the keyboard */
327static int
328wait_for_kbd_data(struct atkbdc_softc *kbdc)
329{
330 /* CPU will stay inside the loop for 200msec at most */
331 int retry = 10000;
332 int port = kbdc->port;
333 int f;
334
335 while ((f = inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
336 != KBDS_KBD_BUFFER_FULL) {
337 if (f == KBDS_AUX_BUFFER_FULL) {
338 DELAY(KBDD_DELAYTIME);
339 addq(&kbdc->aux, inb(port + KBD_DATA_PORT));
340 }
341 DELAY(KBDC_DELAYTIME);
342 if (--retry < 0)
343 return 0;
344 }
345 DELAY(KBDD_DELAYTIME);
346 return f;
347}
348
349/*
350 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
351 * queue anything else.
352 */
353static int
354wait_for_kbd_ack(struct atkbdc_softc *kbdc)
355{
356 /* CPU will stay inside the loop for 200msec at most */
357 int retry = 10000;
358 int port = kbdc->port;
359 int f;
360 int b;
361
362 while (retry-- > 0) {
363 if ((f = inb(port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
364 DELAY(KBDD_DELAYTIME);
365 b = inb(port + KBD_DATA_PORT);
366 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
367 if ((b == KBD_ACK) || (b == KBD_RESEND)
368 || (b == KBD_RESET_FAIL))
369 return b;
370 addq(&kbdc->kbd, b);
371 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
372 addq(&kbdc->aux, b);
373 }

--- 4 unchanged lines hidden (view full) ---

378}
379
380/* wait for data from the aux device */
381static int
382wait_for_aux_data(struct atkbdc_softc *kbdc)
383{
384 /* CPU will stay inside the loop for 200msec at most */
385 int retry = 10000;
386 int port = kbdc->port;
387 int f;
388
389 while ((f = inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
390 != KBDS_AUX_BUFFER_FULL) {
391 if (f == KBDS_KBD_BUFFER_FULL) {
392 DELAY(KBDD_DELAYTIME);
393 addq(&kbdc->kbd, inb(port + KBD_DATA_PORT));
394 }
395 DELAY(KBDC_DELAYTIME);
396 if (--retry < 0)
397 return 0;
398 }
399 DELAY(KBDD_DELAYTIME);
400 return f;
401}
402
403/*
404 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
405 * queue anything else.
406 */
407static int
408wait_for_aux_ack(struct atkbdc_softc *kbdc)
409{
410 /* CPU will stay inside the loop for 200msec at most */
411 int retry = 10000;
412 int port = kbdc->port;
413 int f;
414 int b;
415
416 while (retry-- > 0) {
417 if ((f = inb(port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
418 DELAY(KBDD_DELAYTIME);
419 b = inb(port + KBD_DATA_PORT);
420 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
421 if ((b == PSM_ACK) || (b == PSM_RESEND)
422 || (b == PSM_RESET_FAIL))
423 return b;
424 addq(&kbdc->aux, b);
425 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
426 addq(&kbdc->kbd, b);
427 }

--- 4 unchanged lines hidden (view full) ---

432}
433
434/* write a one byte command to the controller */
435int
436write_controller_command(KBDC p, int c)
437{
438 if (!wait_while_controller_busy(kbdcp(p)))
439 return FALSE;
440 outb(kbdcp(p)->port + KBD_COMMAND_PORT, c);
441 return TRUE;
442}
443
444/* write a one byte data to the controller */
445int
446write_controller_data(KBDC p, int c)
447{
448 if (!wait_while_controller_busy(kbdcp(p)))
449 return FALSE;
450 outb(kbdcp(p)->port + KBD_DATA_PORT, c);
451 return TRUE;
452}
453
454/* write a one byte keyboard command */
455int
456write_kbd_command(KBDC p, int c)
457{
458 if (!wait_while_controller_busy(kbdcp(p)))
459 return FALSE;
460 outb(kbdcp(p)->port + KBD_DATA_PORT, c);
461 return TRUE;
462}
463
464/* write a one byte auxiliary device command */
465int
466write_aux_command(KBDC p, int c)
467{
468 if (!write_controller_command(p, KBDC_WRITE_TO_AUX))

--- 112 unchanged lines hidden (view full) ---

581read_controller_data(KBDC p)
582{
583 if (availq(&kbdcp(p)->kbd))
584 return removeq(&kbdcp(p)->kbd);
585 if (availq(&kbdcp(p)->aux))
586 return removeq(&kbdcp(p)->aux);
587 if (!wait_for_data(kbdcp(p)))
588 return -1; /* timeout */
589 return inb(kbdcp(p)->port + KBD_DATA_PORT);
590}
591
592#if KBDIO_DEBUG >= 2
593static int call = 0;
594#endif
595
596/* read one byte from the keyboard */
597int

--- 8 unchanged lines hidden (view full) ---

606 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
607 }
608#endif
609
610 if (availq(&kbdcp(p)->kbd))
611 return removeq(&kbdcp(p)->kbd);
612 if (!wait_for_kbd_data(kbdcp(p)))
613 return -1; /* timeout */
614 return inb(kbdcp(p)->port + KBD_DATA_PORT);
615}
616
617/* read one byte from the keyboard, but return immediately if
618 * no data is waiting
619 */
620int
621read_kbd_data_no_wait(KBDC p)
622{

--- 6 unchanged lines hidden (view full) ---

629 "aux q: %d calls, max %d chars\n",
630 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount,
631 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount);
632 }
633#endif
634
635 if (availq(&kbdcp(p)->kbd))
636 return removeq(&kbdcp(p)->kbd);
637 f = inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL;
638 if (f == KBDS_AUX_BUFFER_FULL) {
639 DELAY(KBDD_DELAYTIME);
640 addq(&kbdcp(p)->aux, inb(kbdcp(p)->port + KBD_DATA_PORT));
641 f = inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL;
642 }
643 if (f == KBDS_KBD_BUFFER_FULL) {
644 DELAY(KBDD_DELAYTIME);
645 return inb(kbdcp(p)->port + KBD_DATA_PORT);
646 }
647 return -1; /* no data */
648}
649
650/* read one byte from the aux device */
651int
652read_aux_data(KBDC p)
653{
654 if (availq(&kbdcp(p)->aux))
655 return removeq(&kbdcp(p)->aux);
656 if (!wait_for_aux_data(kbdcp(p)))
657 return -1; /* timeout */
658 return inb(kbdcp(p)->port + KBD_DATA_PORT);
659}
660
661/* read one byte from the aux device, but return immediately if
662 * no data is waiting
663 */
664int
665read_aux_data_no_wait(KBDC p)
666{
667 int f;
668
669 if (availq(&kbdcp(p)->aux))
670 return removeq(&kbdcp(p)->aux);
671 f = inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL;
672 if (f == KBDS_KBD_BUFFER_FULL) {
673 DELAY(KBDD_DELAYTIME);
674 addq(&kbdcp(p)->kbd, inb(kbdcp(p)->port + KBD_DATA_PORT));
675 f = inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL;
676 }
677 if (f == KBDS_AUX_BUFFER_FULL) {
678 DELAY(KBDD_DELAYTIME);
679 return inb(kbdcp(p)->port + KBD_DATA_PORT);
680 }
681 return -1; /* no data */
682}
683
684/* discard data from the keyboard */
685void
686empty_kbd_buffer(KBDC p, int wait)
687{
688 int t;
689 int b;
690 int f;
691#if KBDIO_DEBUG >= 2
692 int c1 = 0;
693 int c2 = 0;
694#endif
695 int delta = 2;
696
697 for (t = wait; t > 0; ) {
698 if ((f = inb(kbdcp(p)->port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
699 DELAY(KBDD_DELAYTIME);
700 b = inb(kbdcp(p)->port + KBD_DATA_PORT);
701 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) {
702 addq(&kbdcp(p)->aux, b);
703#if KBDIO_DEBUG >= 2
704 ++c2;
705 } else {
706 ++c1;
707#endif
708 }

--- 20 unchanged lines hidden (view full) ---

729 int f;
730#if KBDIO_DEBUG >= 2
731 int c1 = 0;
732 int c2 = 0;
733#endif
734 int delta = 2;
735
736 for (t = wait; t > 0; ) {
737 if ((f = inb(kbdcp(p)->port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
738 DELAY(KBDD_DELAYTIME);
739 b = inb(kbdcp(p)->port + KBD_DATA_PORT);
740 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) {
741 addq(&kbdcp(p)->kbd, b);
742#if KBDIO_DEBUG >= 2
743 ++c1;
744 } else {
745 ++c2;
746#endif
747 }

--- 19 unchanged lines hidden (view full) ---

767 int f;
768#if KBDIO_DEBUG >= 2
769 int c1 = 0;
770 int c2 = 0;
771#endif
772 int delta = 2;
773
774 for (t = wait; t > 0; ) {
775 if ((f = inb(kbdcp(p)->port + KBD_STATUS_PORT)) & KBDS_ANY_BUFFER_FULL) {
776 DELAY(KBDD_DELAYTIME);
777 (void)inb(kbdcp(p)->port + KBD_DATA_PORT);
778#if KBDIO_DEBUG >= 2
779 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL)
780 ++c1;
781 else
782 ++c2;
783#endif
784 t = wait;
785 } else {

--- 232 unchanged lines hidden ---