2 * Copyright (c) 1996-1999
3 * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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
30 * $FreeBSD: src/sys/dev/kbd/atkbdc.c,v 1.5.2.2 2002/03/31 11:02:02 murray Exp $
31 * $DragonFly: src/sys/dev/misc/kbd/atkbdc.c,v 1.11 2007/04/22 10:54:43 y0netan1 Exp $
32 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp
36 #include "use_atkbdc.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/syslog.h>
45 #include <machine/clock.h>
47 #include "atkbdcreg.h"
49 #include <bus/isa/isareg.h>
53 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */
58 #define MAX(x, y) ((x) > (y) ? (x) : (y))
61 #define kbdcp(p) ((atkbdc_softc_t *)(p))
62 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE)
63 #define availq(q) ((q)->head != (q)->tail)
65 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0)
67 #define emptyq(q) ((q)->tail = (q)->head = 0)
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)))
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
85 static atkbdc_softc_t default_kbdc
;
86 static atkbdc_softc_t
*atkbdc_softc
[MAXKBDC
] = { &default_kbdc
};
88 static int verbose
= KBDIO_DEBUG
;
90 /* function prototypes */
92 static int atkbdc_setup(atkbdc_softc_t
*sc
, bus_space_tag_t tag
,
93 bus_space_handle_t h0
, bus_space_handle_t h1
);
94 static int addq(kbdkqueue
*q
, int c
);
95 static int removeq(kbdkqueue
*q
);
96 static int wait_while_controller_busy(atkbdc_softc_t
*kbdc
);
97 static int wait_for_data(atkbdc_softc_t
*kbdc
);
98 static int wait_for_kbd_data(atkbdc_softc_t
*kbdc
);
99 static int wait_for_kbd_ack(atkbdc_softc_t
*kbdc
);
100 static int wait_for_aux_data(atkbdc_softc_t
*kbdc
);
101 static int wait_for_aux_ack(atkbdc_softc_t
*kbdc
);
104 atkbdc_get_softc(int unit
)
108 if (unit
>= sizeof(atkbdc_softc
)/sizeof(atkbdc_softc
[0]))
110 sc
= atkbdc_softc
[unit
];
112 sc
= kmalloc(sizeof(*sc
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
113 atkbdc_softc
[unit
] = sc
;
119 atkbdc_probe_unit(int unit
, struct resource
*port0
, struct resource
*port1
)
121 if (rman_get_start(port0
) <= 0)
123 if (rman_get_start(port1
) <= 0)
129 atkbdc_attach_unit(int unit
, atkbdc_softc_t
*sc
, struct resource
*port0
,
130 struct resource
*port1
)
132 return atkbdc_setup(sc
, rman_get_bustag(port0
),
133 rman_get_bushandle(port0
),
134 rman_get_bushandle(port1
));
137 /* the backdoor to the keyboard controller! XXX */
139 atkbdc_configure(void)
142 bus_space_handle_t h0
;
143 bus_space_handle_t h1
;
146 #if defined(__i386__)
151 resource_int_value("atkbdc", 0, "port", &port0
);
152 port1
= IO_KBD
+ KBD_STATUS_PORT
;
154 resource_int_value("atkbdc", 0, "port", &port0
);
157 /* XXX: tag should be passed from the caller */
158 #if defined(__i386__)
159 tag
= I386_BUS_SPACE_IO
;
165 bus_space_map(tag
, port0
, IO_KBDSIZE
, 0, &h0
);
166 bus_space_map(tag
, port1
, IO_KBDSIZE
, 0, &h1
);
168 h0
= (bus_space_handle_t
)port0
;
169 h1
= (bus_space_handle_t
)port1
;
172 #if defined(__i386__)
174 * Check if we really have AT keyboard controller. Poll status
175 * register until we get "all clear" indication. If no such
176 * indication comes, it probably means that there is no AT
177 * keyboard controller present. Give up in such case. Check relies
178 * on the fact that reading from non-existing in/out port returns
179 * 0xff on i386. May or may not be true on other platforms.
181 for (i
= 65536; i
!= 0; --i
) {
182 if ((bus_space_read_1(tag
, h1
, 0) & 0x2) == 0)
190 return atkbdc_setup(atkbdc_softc
[0], tag
, h0
, h1
);
194 atkbdc_setup(atkbdc_softc_t
*sc
, bus_space_tag_t tag
, bus_space_handle_t h0
,
195 bus_space_handle_t h1
)
197 if (sc
->ioh0
== 0) { /* XXX */
198 sc
->command_byte
= -1;
199 sc
->command_mask
= 0;
201 sc
->kbd
.head
= sc
->kbd
.tail
= 0;
202 sc
->aux
.head
= sc
->aux
.tail
= 0;
204 sc
->kbd
.call_count
= 0;
205 sc
->kbd
.qcount
= sc
->kbd
.max_qcount
= 0;
206 sc
->aux
.call_count
= 0;
207 sc
->aux
.qcount
= sc
->aux
.max_qcount
= 0;
216 /* open a keyboard controller */
218 atkbdc_open(int unit
)
224 if ((atkbdc_softc
[unit
]->port0
!= NULL
)
225 || (atkbdc_softc
[unit
]->ioh0
!= 0)) /* XXX */
226 return (KBDC
)atkbdc_softc
[unit
];
231 * I/O access arbitration in `kbdio'
233 * The `kbdio' module uses a simplistic convention to arbitrate
234 * I/O access to the controller/keyboard/mouse. The convention requires
235 * close cooperation of the calling device driver.
237 * The device drivers which utilize the `kbdio' module are assumed to
238 * have the following set of routines.
239 * a. An interrupt handler (the bottom half of the driver).
240 * b. Timeout routines which may briefly poll the keyboard controller.
241 * c. Routines outside interrupt context (the top half of the driver).
242 * They should follow the rules below:
243 * 1. The interrupt handler may assume that it always has full access
244 * to the controller/keyboard/mouse.
245 * 2. The other routines must issue `spltty()' if they wish to
246 * prevent the interrupt handler from accessing
247 * the controller/keyboard/mouse.
248 * 3. The timeout routines and the top half routines of the device driver
249 * arbitrate I/O access by observing the lock flag in `kbdio'.
250 * The flag is manipulated via `kbdc_lock()'; when one wants to
251 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
252 * the call returns with TRUE. Otherwise the caller must back off.
253 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
254 * is finished. This mechanism does not prevent the interrupt
255 * handler from being invoked at any time and carrying out I/O.
256 * Therefore, `spltty()' must be strategically placed in the device
257 * driver code. Also note that the timeout routine may interrupt
258 * `kbdc_lock()' called by the top half of the driver, but this
259 * interruption is OK so long as the timeout routine observes
261 * 4. The interrupt and timeout routines should not extend I/O operation
262 * across more than one interrupt or timeout; they must complete any
263 * necessary I/O operation within one invocation of the routine.
264 * This means that if the timeout routine acquires the lock flag,
265 * it must reset the flag to FALSE before it returns.
268 /* set/reset polling lock */
270 kbdc_lock(KBDC p
, int lock
)
274 prevlock
= kbdcp(p
)->lock
;
275 kbdcp(p
)->lock
= lock
;
277 return (prevlock
!= lock
);
280 /* check if any data is waiting to be processed */
282 kbdc_data_ready(KBDC p
)
284 return (availq(&kbdcp(p
)->kbd
) || availq(&kbdcp(p
)->aux
)
285 || (read_status(kbdcp(p
)) & KBDS_ANY_BUFFER_FULL
));
288 /* queuing functions */
291 addq(kbdkqueue
*q
, int c
)
293 if (nextq(q
->tail
) != q
->head
) {
295 q
->tail
= nextq(q
->tail
);
299 if (q
->qcount
> q
->max_qcount
)
300 q
->max_qcount
= q
->qcount
;
308 removeq(kbdkqueue
*q
)
312 if (q
->tail
!= q
->head
) {
314 q
->head
= nextq(q
->head
);
324 * device I/O routines
327 wait_while_controller_busy(struct atkbdc_softc
*kbdc
)
329 /* CPU will stay inside the loop for 100msec at most */
333 while ((f
= read_status(kbdc
)) & KBDS_INPUT_BUFFER_FULL
) {
334 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
335 DELAY(KBDD_DELAYTIME
);
336 addq(&kbdc
->kbd
, read_data(kbdc
));
337 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
338 DELAY(KBDD_DELAYTIME
);
339 addq(&kbdc
->aux
, read_data(kbdc
));
341 DELAY(KBDC_DELAYTIME
);
349 * wait for any data; whether it's from the controller,
350 * the keyboard, or the aux device.
353 wait_for_data(struct atkbdc_softc
*kbdc
)
355 /* CPU will stay inside the loop for 200msec at most */
359 while ((f
= read_status(kbdc
) & KBDS_ANY_BUFFER_FULL
) == 0) {
360 DELAY(KBDC_DELAYTIME
);
364 DELAY(KBDD_DELAYTIME
);
368 /* wait for data from the keyboard */
370 wait_for_kbd_data(struct atkbdc_softc
*kbdc
)
372 /* CPU will stay inside the loop for 200msec at most */
376 while ((f
= read_status(kbdc
) & KBDS_BUFFER_FULL
)
377 != KBDS_KBD_BUFFER_FULL
) {
378 if (f
== KBDS_AUX_BUFFER_FULL
) {
379 DELAY(KBDD_DELAYTIME
);
380 addq(&kbdc
->aux
, read_data(kbdc
));
382 DELAY(KBDC_DELAYTIME
);
386 DELAY(KBDD_DELAYTIME
);
391 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
392 * queue anything else.
395 wait_for_kbd_ack(struct atkbdc_softc
*kbdc
)
397 /* CPU will stay inside the loop for 200msec at most */
402 while (retry
-- > 0) {
403 if ((f
= read_status(kbdc
)) & KBDS_ANY_BUFFER_FULL
) {
404 DELAY(KBDD_DELAYTIME
);
406 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
407 if ((b
== KBD_ACK
) || (b
== KBD_RESEND
)
408 || (b
== KBD_RESET_FAIL
))
411 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
415 DELAY(KBDC_DELAYTIME
);
420 /* wait for data from the aux device */
422 wait_for_aux_data(struct atkbdc_softc
*kbdc
)
424 /* CPU will stay inside the loop for 200msec at most */
428 while ((f
= read_status(kbdc
) & KBDS_BUFFER_FULL
)
429 != KBDS_AUX_BUFFER_FULL
) {
430 if (f
== KBDS_KBD_BUFFER_FULL
) {
431 DELAY(KBDD_DELAYTIME
);
432 addq(&kbdc
->kbd
, read_data(kbdc
));
434 DELAY(KBDC_DELAYTIME
);
438 DELAY(KBDD_DELAYTIME
);
443 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
444 * queue anything else.
447 wait_for_aux_ack(struct atkbdc_softc
*kbdc
)
449 /* CPU will stay inside the loop for 200msec at most */
454 while (retry
-- > 0) {
455 if ((f
= read_status(kbdc
)) & KBDS_ANY_BUFFER_FULL
) {
456 DELAY(KBDD_DELAYTIME
);
458 if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
459 if ((b
== PSM_ACK
) || (b
== PSM_RESEND
)
460 || (b
== PSM_RESET_FAIL
))
463 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
467 DELAY(KBDC_DELAYTIME
);
472 /* write a one byte command to the controller */
474 write_controller_command(KBDC p
, int c
)
476 if (!wait_while_controller_busy(kbdcp(p
)))
478 write_command(kbdcp(p
), c
);
482 /* write a one byte data to the controller */
484 write_controller_data(KBDC p
, int c
)
486 if (!wait_while_controller_busy(kbdcp(p
)))
488 write_data(kbdcp(p
), c
);
492 /* write a one byte keyboard command */
494 write_kbd_command(KBDC p
, int c
)
496 if (!wait_while_controller_busy(kbdcp(p
)))
498 write_data(kbdcp(p
), c
);
502 /* write a one byte auxiliary device command */
504 write_aux_command(KBDC p
, int c
)
506 if (!write_controller_command(p
, KBDC_WRITE_TO_AUX
))
508 return write_controller_data(p
, c
);
511 /* send a command to the keyboard and wait for ACK */
513 send_kbd_command(KBDC p
, int c
)
515 int retry
= KBD_MAXRETRY
;
518 while (retry
-- > 0) {
519 if (!write_kbd_command(p
, c
))
521 res
= wait_for_kbd_ack(kbdcp(p
));
528 /* send a command to the auxiliary device and wait for ACK */
530 send_aux_command(KBDC p
, int c
)
532 int retry
= KBD_MAXRETRY
;
535 while (retry
-- > 0) {
536 if (!write_aux_command(p
, c
))
540 * The aux device may have already sent one or two bytes of
541 * status data, when a command is received. It will immediately
542 * stop data transmission, thus, leaving an incomplete data
543 * packet in our buffer. We have to discard any unprocessed
544 * data in order to remove such packets. Well, we may remove
545 * unprocessed, but necessary data byte as well...
547 emptyq(&kbdcp(p
)->aux
);
548 res
= wait_for_aux_ack(kbdcp(p
));
555 /* send a command and a data to the keyboard, wait for ACKs */
557 send_kbd_command_and_data(KBDC p
, int c
, int d
)
562 for (retry
= KBD_MAXRETRY
; retry
> 0; --retry
) {
563 if (!write_kbd_command(p
, c
))
565 res
= wait_for_kbd_ack(kbdcp(p
));
568 else if (res
!= KBD_RESEND
)
574 for (retry
= KBD_MAXRETRY
, res
= -1; retry
> 0; --retry
) {
575 if (!write_kbd_command(p
, d
))
577 res
= wait_for_kbd_ack(kbdcp(p
));
578 if (res
!= KBD_RESEND
)
584 /* send a command and a data to the auxiliary device, wait for ACKs */
586 send_aux_command_and_data(KBDC p
, int c
, int d
)
591 for (retry
= KBD_MAXRETRY
; retry
> 0; --retry
) {
592 if (!write_aux_command(p
, c
))
594 emptyq(&kbdcp(p
)->aux
);
595 res
= wait_for_aux_ack(kbdcp(p
));
598 else if (res
!= PSM_RESEND
)
604 for (retry
= KBD_MAXRETRY
, res
= -1; retry
> 0; --retry
) {
605 if (!write_aux_command(p
, d
))
607 res
= wait_for_aux_ack(kbdcp(p
));
608 if (res
!= PSM_RESEND
)
615 * read one byte from any source; whether from the controller,
616 * the keyboard, or the aux device
619 read_controller_data(KBDC p
)
621 if (availq(&kbdcp(p
)->kbd
))
622 return removeq(&kbdcp(p
)->kbd
);
623 if (availq(&kbdcp(p
)->aux
))
624 return removeq(&kbdcp(p
)->aux
);
625 if (!wait_for_data(kbdcp(p
)))
626 return -1; /* timeout */
627 return read_data(kbdcp(p
));
634 /* read one byte from the keyboard */
636 read_kbd_data(KBDC p
)
641 log(LOG_DEBUG
, "kbdc: kbd q: %d calls, max %d chars, "
642 "aux q: %d calls, max %d chars\n",
643 kbdcp(p
)->kbd
.call_count
, kbdcp(p
)->kbd
.max_qcount
,
644 kbdcp(p
)->aux
.call_count
, kbdcp(p
)->aux
.max_qcount
);
648 if (availq(&kbdcp(p
)->kbd
))
649 return removeq(&kbdcp(p
)->kbd
);
650 if (!wait_for_kbd_data(kbdcp(p
)))
651 return -1; /* timeout */
652 return read_data(kbdcp(p
));
655 /* read one byte from the keyboard, but return immediately if
659 read_kbd_data_no_wait(KBDC p
)
666 log(LOG_DEBUG
, "kbdc: kbd q: %d calls, max %d chars, "
667 "aux q: %d calls, max %d chars\n",
668 kbdcp(p
)->kbd
.call_count
, kbdcp(p
)->kbd
.max_qcount
,
669 kbdcp(p
)->aux
.call_count
, kbdcp(p
)->aux
.max_qcount
);
673 if (availq(&kbdcp(p
)->kbd
))
674 return removeq(&kbdcp(p
)->kbd
);
675 f
= read_status(kbdcp(p
)) & KBDS_BUFFER_FULL
;
676 if (f
== KBDS_AUX_BUFFER_FULL
) {
677 DELAY(KBDD_DELAYTIME
);
678 addq(&kbdcp(p
)->aux
, read_data(kbdcp(p
)));
679 f
= read_status(kbdcp(p
)) & KBDS_BUFFER_FULL
;
681 if (f
== KBDS_KBD_BUFFER_FULL
) {
682 DELAY(KBDD_DELAYTIME
);
683 return read_data(kbdcp(p
));
685 return -1; /* no data */
688 /* read one byte from the aux device */
690 read_aux_data(KBDC p
)
692 if (availq(&kbdcp(p
)->aux
))
693 return removeq(&kbdcp(p
)->aux
);
694 if (!wait_for_aux_data(kbdcp(p
)))
695 return -1; /* timeout */
696 return read_data(kbdcp(p
));
699 /* read one byte from the aux device, but return immediately if
703 read_aux_data_no_wait(KBDC p
)
707 if (availq(&kbdcp(p
)->aux
))
708 return removeq(&kbdcp(p
)->aux
);
709 f
= read_status(kbdcp(p
)) & KBDS_BUFFER_FULL
;
710 if (f
== KBDS_KBD_BUFFER_FULL
) {
711 DELAY(KBDD_DELAYTIME
);
712 addq(&kbdcp(p
)->kbd
, read_data(kbdcp(p
)));
713 f
= read_status(kbdcp(p
)) & KBDS_BUFFER_FULL
;
715 if (f
== KBDS_AUX_BUFFER_FULL
) {
716 DELAY(KBDD_DELAYTIME
);
717 return read_data(kbdcp(p
));
719 return -1; /* no data */
722 /* discard data from the keyboard */
724 empty_kbd_buffer(KBDC p
, int wait
)
735 for (t
= wait
; t
> 0; ) {
736 if ((f
= read_status(kbdcp(p
))) & KBDS_ANY_BUFFER_FULL
) {
737 DELAY(KBDD_DELAYTIME
);
738 b
= read_data(kbdcp(p
));
739 if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
740 addq(&kbdcp(p
)->aux
, b
);
754 if ((c1
> 0) || (c2
> 0))
755 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1
, c2
);
758 emptyq(&kbdcp(p
)->kbd
);
761 /* discard data from the aux device */
763 empty_aux_buffer(KBDC p
, int wait
)
774 for (t
= wait
; t
> 0; ) {
775 if ((f
= read_status(kbdcp(p
))) & KBDS_ANY_BUFFER_FULL
) {
776 DELAY(KBDD_DELAYTIME
);
777 b
= read_data(kbdcp(p
));
778 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
779 addq(&kbdcp(p
)->kbd
, b
);
793 if ((c1
> 0) || (c2
> 0))
794 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1
, c2
);
797 emptyq(&kbdcp(p
)->aux
);
800 /* discard any data from the keyboard or the aux device */
802 empty_both_buffers(KBDC p
, int wait
)
812 for (t
= wait
; t
> 0; ) {
813 if ((f
= read_status(kbdcp(p
))) & KBDS_ANY_BUFFER_FULL
) {
814 DELAY(KBDD_DELAYTIME
);
815 (void)read_data(kbdcp(p
));
817 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
)
829 if ((c1
> 0) || (c2
> 0))
830 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_both_buffers)\n", c1
, c2
);
833 emptyq(&kbdcp(p
)->kbd
);
834 emptyq(&kbdcp(p
)->aux
);
837 /* keyboard and mouse device control */
839 /* NOTE: enable the keyboard port but disable the keyboard
840 * interrupt before calling "reset_kbd()".
845 int retry
= KBD_MAXRETRY
;
846 int again
= KBD_MAXWAIT
;
847 int c
= KBD_RESEND
; /* keep the compiler happy */
849 while (retry
-- > 0) {
850 empty_both_buffers(p
, 10);
851 if (!write_kbd_command(p
, KBDC_RESET_KBD
))
853 emptyq(&kbdcp(p
)->kbd
);
854 c
= read_controller_data(p
);
855 if (verbose
|| bootverbose
)
856 log(LOG_DEBUG
, "kbdc: RESET_KBD return code:%04x\n", c
);
857 if (c
== KBD_ACK
) /* keyboard has agreed to reset itself... */
863 while (again
-- > 0) {
864 /* wait awhile, well, in fact we must wait quite loooooooooooong */
865 DELAY(KBD_RESETDELAY
*1000);
866 c
= read_controller_data(p
); /* RESET_DONE/RESET_FAIL */
867 if (c
!= -1) /* wait again if the controller is not ready */
870 if (verbose
|| bootverbose
)
871 log(LOG_DEBUG
, "kbdc: RESET_KBD status:%04x\n", c
);
872 if (c
!= KBD_RESET_DONE
)
877 /* NOTE: enable the aux port but disable the aux interrupt
878 * before calling `reset_aux_dev()'.
881 reset_aux_dev(KBDC p
)
883 int retry
= KBD_MAXRETRY
;
884 int again
= KBD_MAXWAIT
;
885 int c
= PSM_RESEND
; /* keep the compiler happy */
887 while (retry
-- > 0) {
888 empty_both_buffers(p
, 10);
889 if (!write_aux_command(p
, PSMC_RESET_DEV
))
891 emptyq(&kbdcp(p
)->aux
);
892 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
893 for (again
= KBD_MAXWAIT
; again
> 0; --again
) {
894 DELAY(KBD_RESETDELAY
*1000);
895 c
= read_aux_data_no_wait(p
);
899 if (verbose
|| bootverbose
)
900 log(LOG_DEBUG
, "kbdc: RESET_AUX return code:%04x\n", c
);
901 if (c
== PSM_ACK
) /* aux dev is about to reset... */
907 for (again
= KBD_MAXWAIT
; again
> 0; --again
) {
908 /* wait awhile, well, quite looooooooooooong */
909 DELAY(KBD_RESETDELAY
*1000);
910 c
= read_aux_data_no_wait(p
); /* RESET_DONE/RESET_FAIL */
911 if (c
!= -1) /* wait again if the controller is not ready */
914 if (verbose
|| bootverbose
)
915 log(LOG_DEBUG
, "kbdc: RESET_AUX status:%04x\n", c
);
916 if (c
!= PSM_RESET_DONE
) /* reset status */
919 c
= read_aux_data(p
); /* device ID */
920 if (verbose
|| bootverbose
)
921 log(LOG_DEBUG
, "kbdc: RESET_AUX ID:%04x\n", c
);
922 /* NOTE: we could check the device ID now, but leave it later... */
926 /* controller diagnostics and setup */
929 test_controller(KBDC p
)
931 int retry
= KBD_MAXRETRY
;
932 int again
= KBD_MAXWAIT
;
933 int c
= KBD_DIAG_FAIL
;
935 while (retry
-- > 0) {
936 empty_both_buffers(p
, 10);
937 if (write_controller_command(p
, KBDC_DIAGNOSE
))
943 emptyq(&kbdcp(p
)->kbd
);
944 while (again
-- > 0) {
946 DELAY(KBD_RESETDELAY
*1000);
947 c
= read_controller_data(p
); /* DIAG_DONE/DIAG_FAIL */
948 if (c
!= -1) /* wait again if the controller is not ready */
951 if (verbose
|| bootverbose
)
952 log(LOG_DEBUG
, "kbdc: DIAGNOSE status:%04x\n", c
);
953 return (c
== KBD_DIAG_DONE
);
957 test_kbd_port(KBDC p
)
959 int retry
= KBD_MAXRETRY
;
960 int again
= KBD_MAXWAIT
;
963 while (retry
-- > 0) {
964 empty_both_buffers(p
, 10);
965 if (write_controller_command(p
, KBDC_TEST_KBD_PORT
))
971 emptyq(&kbdcp(p
)->kbd
);
972 while (again
-- > 0) {
973 c
= read_controller_data(p
);
974 if (c
!= -1) /* try again if the controller is not ready */
977 if (verbose
|| bootverbose
)
978 log(LOG_DEBUG
, "kbdc: TEST_KBD_PORT status:%04x\n", c
);
983 test_aux_port(KBDC p
)
985 int retry
= KBD_MAXRETRY
;
986 int again
= KBD_MAXWAIT
;
989 while (retry
-- > 0) {
990 empty_both_buffers(p
, 10);
991 if (write_controller_command(p
, KBDC_TEST_AUX_PORT
))
997 emptyq(&kbdcp(p
)->kbd
);
998 while (again
-- > 0) {
999 c
= read_controller_data(p
);
1000 if (c
!= -1) /* try again if the controller is not ready */
1003 if (verbose
|| bootverbose
)
1004 log(LOG_DEBUG
, "kbdc: TEST_AUX_PORT status:%04x\n", c
);
1009 kbdc_get_device_mask(KBDC p
)
1011 return kbdcp(p
)->command_mask
;
1015 kbdc_set_device_mask(KBDC p
, int mask
)
1017 kbdcp(p
)->command_mask
=
1018 mask
& (KBD_KBD_CONTROL_BITS
| KBD_AUX_CONTROL_BITS
);
1022 get_controller_command_byte(KBDC p
)
1024 if (kbdcp(p
)->command_byte
!= -1)
1025 return kbdcp(p
)->command_byte
;
1026 if (!write_controller_command(p
, KBDC_GET_COMMAND_BYTE
))
1028 emptyq(&kbdcp(p
)->kbd
);
1029 kbdcp(p
)->command_byte
= read_controller_data(p
);
1030 return kbdcp(p
)->command_byte
;
1034 set_controller_command_byte(KBDC p
, int mask
, int command
)
1036 if (get_controller_command_byte(p
) == -1)
1039 command
= (kbdcp(p
)->command_byte
& ~mask
) | (command
& mask
);
1040 if (command
& KBD_DISABLE_KBD_PORT
) {
1041 if (!write_controller_command(p
, KBDC_DISABLE_KBD_PORT
))
1044 if (!write_controller_command(p
, KBDC_SET_COMMAND_BYTE
))
1046 if (!write_controller_data(p
, command
))
1048 kbdcp(p
)->command_byte
= command
;
1051 log(LOG_DEBUG
, "kbdc: new command byte:%04x (set_controller...)\n",