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.8 2006/10/25 20:55:54 dillon 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(kqueue
*q
, int c
);
95 static int removeq(kqueue
*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
;
148 resource_int_value("atkbdc", 0, "port", &port0
);
149 port1
= IO_KBD
+ KBD_STATUS_PORT
;
151 resource_int_value("atkbdc", 0, "port", &port0
);
154 /* XXX: tag should be passed from the caller */
155 #if defined(__i386__)
156 tag
= I386_BUS_SPACE_IO
;
160 bus_space_map(tag
, port0
, IO_KBDSIZE
, 0, &h0
);
161 bus_space_map(tag
, port1
, IO_KBDSIZE
, 0, &h1
);
163 h0
= (bus_space_handle_t
)port0
;
164 h1
= (bus_space_handle_t
)port1
;
166 return atkbdc_setup(atkbdc_softc
[0], tag
, h0
, h1
);
170 atkbdc_setup(atkbdc_softc_t
*sc
, bus_space_tag_t tag
, bus_space_handle_t h0
,
171 bus_space_handle_t h1
)
173 if (sc
->ioh0
== 0) { /* XXX */
174 sc
->command_byte
= -1;
175 sc
->command_mask
= 0;
177 sc
->kbd
.head
= sc
->kbd
.tail
= 0;
178 sc
->aux
.head
= sc
->aux
.tail
= 0;
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;
192 /* open a keyboard controller */
194 atkbdc_open(int unit
)
200 if ((atkbdc_softc
[unit
]->port0
!= NULL
)
201 || (atkbdc_softc
[unit
]->ioh0
!= 0)) /* XXX */
202 return (KBDC
)atkbdc_softc
[unit
];
207 * I/O access arbitration in `kbdio'
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.
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
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.
244 /* set/reset polling lock */
246 kbdc_lock(KBDC p
, int lock
)
250 prevlock
= kbdcp(p
)->lock
;
251 kbdcp(p
)->lock
= lock
;
253 return (prevlock
!= lock
);
256 /* check if any data is waiting to be processed */
258 kbdc_data_ready(KBDC p
)
260 return (availq(&kbdcp(p
)->kbd
) || availq(&kbdcp(p
)->aux
)
261 || (read_status(kbdcp(p
)) & KBDS_ANY_BUFFER_FULL
));
264 /* queuing functions */
267 addq(kqueue
*q
, int c
)
269 if (nextq(q
->tail
) != q
->head
) {
271 q
->tail
= nextq(q
->tail
);
275 if (q
->qcount
> q
->max_qcount
)
276 q
->max_qcount
= q
->qcount
;
288 if (q
->tail
!= q
->head
) {
290 q
->head
= nextq(q
->head
);
300 * device I/O routines
303 wait_while_controller_busy(struct atkbdc_softc
*kbdc
)
305 /* CPU will stay inside the loop for 100msec at most */
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
));
317 DELAY(KBDC_DELAYTIME
);
325 * wait for any data; whether it's from the controller,
326 * the keyboard, or the aux device.
329 wait_for_data(struct atkbdc_softc
*kbdc
)
331 /* CPU will stay inside the loop for 200msec at most */
335 while ((f
= read_status(kbdc
) & KBDS_ANY_BUFFER_FULL
) == 0) {
336 DELAY(KBDC_DELAYTIME
);
340 DELAY(KBDD_DELAYTIME
);
344 /* wait for data from the keyboard */
346 wait_for_kbd_data(struct atkbdc_softc
*kbdc
)
348 /* CPU will stay inside the loop for 200msec at most */
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
));
358 DELAY(KBDC_DELAYTIME
);
362 DELAY(KBDD_DELAYTIME
);
367 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
368 * queue anything else.
371 wait_for_kbd_ack(struct atkbdc_softc
*kbdc
)
373 /* CPU will stay inside the loop for 200msec at most */
378 while (retry
-- > 0) {
379 if ((f
= read_status(kbdc
)) & KBDS_ANY_BUFFER_FULL
) {
380 DELAY(KBDD_DELAYTIME
);
382 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
383 if ((b
== KBD_ACK
) || (b
== KBD_RESEND
)
384 || (b
== KBD_RESET_FAIL
))
387 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
391 DELAY(KBDC_DELAYTIME
);
396 /* wait for data from the aux device */
398 wait_for_aux_data(struct atkbdc_softc
*kbdc
)
400 /* CPU will stay inside the loop for 200msec at most */
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
));
410 DELAY(KBDC_DELAYTIME
);
414 DELAY(KBDD_DELAYTIME
);
419 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
420 * queue anything else.
423 wait_for_aux_ack(struct atkbdc_softc
*kbdc
)
425 /* CPU will stay inside the loop for 200msec at most */
430 while (retry
-- > 0) {
431 if ((f
= read_status(kbdc
)) & KBDS_ANY_BUFFER_FULL
) {
432 DELAY(KBDD_DELAYTIME
);
434 if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
435 if ((b
== PSM_ACK
) || (b
== PSM_RESEND
)
436 || (b
== PSM_RESET_FAIL
))
439 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
443 DELAY(KBDC_DELAYTIME
);
448 /* write a one byte command to the controller */
450 write_controller_command(KBDC p
, int c
)
452 if (!wait_while_controller_busy(kbdcp(p
)))
454 write_command(kbdcp(p
), c
);
458 /* write a one byte data to the controller */
460 write_controller_data(KBDC p
, int c
)
462 if (!wait_while_controller_busy(kbdcp(p
)))
464 write_data(kbdcp(p
), c
);
468 /* write a one byte keyboard command */
470 write_kbd_command(KBDC p
, int c
)
472 if (!wait_while_controller_busy(kbdcp(p
)))
474 write_data(kbdcp(p
), c
);
478 /* write a one byte auxiliary device command */
480 write_aux_command(KBDC p
, int c
)
482 if (!write_controller_command(p
, KBDC_WRITE_TO_AUX
))
484 return write_controller_data(p
, c
);
487 /* send a command to the keyboard and wait for ACK */
489 send_kbd_command(KBDC p
, int c
)
491 int retry
= KBD_MAXRETRY
;
494 while (retry
-- > 0) {
495 if (!write_kbd_command(p
, c
))
497 res
= wait_for_kbd_ack(kbdcp(p
));
504 /* send a command to the auxiliary device and wait for ACK */
506 send_aux_command(KBDC p
, int c
)
508 int retry
= KBD_MAXRETRY
;
511 while (retry
-- > 0) {
512 if (!write_aux_command(p
, c
))
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...
523 emptyq(&kbdcp(p
)->aux
);
524 res
= wait_for_aux_ack(kbdcp(p
));
531 /* send a command and a data to the keyboard, wait for ACKs */
533 send_kbd_command_and_data(KBDC p
, int c
, int d
)
538 for (retry
= KBD_MAXRETRY
; retry
> 0; --retry
) {
539 if (!write_kbd_command(p
, c
))
541 res
= wait_for_kbd_ack(kbdcp(p
));
544 else if (res
!= KBD_RESEND
)
550 for (retry
= KBD_MAXRETRY
, res
= -1; retry
> 0; --retry
) {
551 if (!write_kbd_command(p
, d
))
553 res
= wait_for_kbd_ack(kbdcp(p
));
554 if (res
!= KBD_RESEND
)
560 /* send a command and a data to the auxiliary device, wait for ACKs */
562 send_aux_command_and_data(KBDC p
, int c
, int d
)
567 for (retry
= KBD_MAXRETRY
; retry
> 0; --retry
) {
568 if (!write_aux_command(p
, c
))
570 emptyq(&kbdcp(p
)->aux
);
571 res
= wait_for_aux_ack(kbdcp(p
));
574 else if (res
!= PSM_RESEND
)
580 for (retry
= KBD_MAXRETRY
, res
= -1; retry
> 0; --retry
) {
581 if (!write_aux_command(p
, d
))
583 res
= wait_for_aux_ack(kbdcp(p
));
584 if (res
!= PSM_RESEND
)
591 * read one byte from any source; whether from the controller,
592 * the keyboard, or the aux device
595 read_controller_data(KBDC p
)
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
));
610 /* read one byte from the keyboard */
612 read_kbd_data(KBDC p
)
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
);
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
));
631 /* read one byte from the keyboard, but return immediately if
635 read_kbd_data_no_wait(KBDC p
)
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
);
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
;
657 if (f
== KBDS_KBD_BUFFER_FULL
) {
658 DELAY(KBDD_DELAYTIME
);
659 return read_data(kbdcp(p
));
661 return -1; /* no data */
664 /* read one byte from the aux device */
666 read_aux_data(KBDC p
)
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
));
675 /* read one byte from the aux device, but return immediately if
679 read_aux_data_no_wait(KBDC p
)
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
;
691 if (f
== KBDS_AUX_BUFFER_FULL
) {
692 DELAY(KBDD_DELAYTIME
);
693 return read_data(kbdcp(p
));
695 return -1; /* no data */
698 /* discard data from the keyboard */
700 empty_kbd_buffer(KBDC p
, int wait
)
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
);
730 if ((c1
> 0) || (c2
> 0))
731 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1
, c2
);
734 emptyq(&kbdcp(p
)->kbd
);
737 /* discard data from the aux device */
739 empty_aux_buffer(KBDC p
, int wait
)
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
);
769 if ((c1
> 0) || (c2
> 0))
770 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1
, c2
);
773 emptyq(&kbdcp(p
)->aux
);
776 /* discard any data from the keyboard or the aux device */
778 empty_both_buffers(KBDC p
, int wait
)
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
));
793 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
)
805 if ((c1
> 0) || (c2
> 0))
806 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_both_buffers)\n", c1
, c2
);
809 emptyq(&kbdcp(p
)->kbd
);
810 emptyq(&kbdcp(p
)->aux
);
813 /* keyboard and mouse device control */
815 /* NOTE: enable the keyboard port but disable the keyboard
816 * interrupt before calling "reset_kbd()".
821 int retry
= KBD_MAXRETRY
;
822 int again
= KBD_MAXWAIT
;
823 int c
= KBD_RESEND
; /* keep the compiler happy */
825 while (retry
-- > 0) {
826 empty_both_buffers(p
, 10);
827 if (!write_kbd_command(p
, KBDC_RESET_KBD
))
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... */
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 */
846 if (verbose
|| bootverbose
)
847 log(LOG_DEBUG
, "kbdc: RESET_KBD status:%04x\n", c
);
848 if (c
!= KBD_RESET_DONE
)
853 /* NOTE: enable the aux port but disable the aux interrupt
854 * before calling `reset_aux_dev()'.
857 reset_aux_dev(KBDC p
)
859 int retry
= KBD_MAXRETRY
;
860 int again
= KBD_MAXWAIT
;
861 int c
= PSM_RESEND
; /* keep the compiler happy */
863 while (retry
-- > 0) {
864 empty_both_buffers(p
, 10);
865 if (!write_aux_command(p
, PSMC_RESET_DEV
))
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
);
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... */
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 */
890 if (verbose
|| bootverbose
)
891 log(LOG_DEBUG
, "kbdc: RESET_AUX status:%04x\n", c
);
892 if (c
!= PSM_RESET_DONE
) /* reset status */
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... */
902 /* controller diagnostics and setup */
905 test_controller(KBDC p
)
907 int retry
= KBD_MAXRETRY
;
908 int again
= KBD_MAXWAIT
;
909 int c
= KBD_DIAG_FAIL
;
911 while (retry
-- > 0) {
912 empty_both_buffers(p
, 10);
913 if (write_controller_command(p
, KBDC_DIAGNOSE
))
919 emptyq(&kbdcp(p
)->kbd
);
920 while (again
-- > 0) {
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 */
927 if (verbose
|| bootverbose
)
928 log(LOG_DEBUG
, "kbdc: DIAGNOSE status:%04x\n", c
);
929 return (c
== KBD_DIAG_DONE
);
933 test_kbd_port(KBDC p
)
935 int retry
= KBD_MAXRETRY
;
936 int again
= KBD_MAXWAIT
;
939 while (retry
-- > 0) {
940 empty_both_buffers(p
, 10);
941 if (write_controller_command(p
, KBDC_TEST_KBD_PORT
))
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 */
953 if (verbose
|| bootverbose
)
954 log(LOG_DEBUG
, "kbdc: TEST_KBD_PORT status:%04x\n", c
);
959 test_aux_port(KBDC p
)
961 int retry
= KBD_MAXRETRY
;
962 int again
= KBD_MAXWAIT
;
965 while (retry
-- > 0) {
966 empty_both_buffers(p
, 10);
967 if (write_controller_command(p
, KBDC_TEST_AUX_PORT
))
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 */
979 if (verbose
|| bootverbose
)
980 log(LOG_DEBUG
, "kbdc: TEST_AUX_PORT status:%04x\n", c
);
985 kbdc_get_device_mask(KBDC p
)
987 return kbdcp(p
)->command_mask
;
991 kbdc_set_device_mask(KBDC p
, int mask
)
993 kbdcp(p
)->command_mask
=
994 mask
& (KBD_KBD_CONTROL_BITS
| KBD_AUX_CONTROL_BITS
);
998 get_controller_command_byte(KBDC p
)
1000 if (kbdcp(p
)->command_byte
!= -1)
1001 return kbdcp(p
)->command_byte
;
1002 if (!write_controller_command(p
, KBDC_GET_COMMAND_BYTE
))
1004 emptyq(&kbdcp(p
)->kbd
);
1005 kbdcp(p
)->command_byte
= read_controller_data(p
);
1006 return kbdcp(p
)->command_byte
;
1010 set_controller_command_byte(KBDC p
, int mask
, int command
)
1012 if (get_controller_command_byte(p
) == -1)
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
))
1020 if (!write_controller_command(p
, KBDC_SET_COMMAND_BYTE
))
1022 if (!write_controller_data(p
, command
))
1024 kbdcp(p
)->command_byte
= command
;
1027 log(LOG_DEBUG
, "kbdc: new command byte:%04x (set_controller...)\n",