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
;
163 bus_space_map(tag
, port0
, IO_KBDSIZE
, 0, &h0
);
164 bus_space_map(tag
, port1
, IO_KBDSIZE
, 0, &h1
);
166 h0
= (bus_space_handle_t
)port0
;
167 h1
= (bus_space_handle_t
)port1
;
170 #if defined(__i386__)
172 * Check if we really have AT keyboard controller. Poll status
173 * register until we get "all clear" indication. If no such
174 * indication comes, it probably means that there is no AT
175 * keyboard controller present. Give up in such case. Check relies
176 * on the fact that reading from non-existing in/out port returns
177 * 0xff on i386. May or may not be true on other platforms.
179 for (i
= 65536; i
!= 0; --i
) {
180 if ((bus_space_read_1(tag
, h1
, 0) & 0x2) == 0)
188 return atkbdc_setup(atkbdc_softc
[0], tag
, h0
, h1
);
192 atkbdc_setup(atkbdc_softc_t
*sc
, bus_space_tag_t tag
, bus_space_handle_t h0
,
193 bus_space_handle_t h1
)
195 if (sc
->ioh0
== 0) { /* XXX */
196 sc
->command_byte
= -1;
197 sc
->command_mask
= 0;
199 sc
->kbd
.head
= sc
->kbd
.tail
= 0;
200 sc
->aux
.head
= sc
->aux
.tail
= 0;
202 sc
->kbd
.call_count
= 0;
203 sc
->kbd
.qcount
= sc
->kbd
.max_qcount
= 0;
204 sc
->aux
.call_count
= 0;
205 sc
->aux
.qcount
= sc
->aux
.max_qcount
= 0;
214 /* open a keyboard controller */
216 atkbdc_open(int unit
)
222 if ((atkbdc_softc
[unit
]->port0
!= NULL
)
223 || (atkbdc_softc
[unit
]->ioh0
!= 0)) /* XXX */
224 return (KBDC
)atkbdc_softc
[unit
];
229 * I/O access arbitration in `kbdio'
231 * The `kbdio' module uses a simplistic convention to arbitrate
232 * I/O access to the controller/keyboard/mouse. The convention requires
233 * close cooperation of the calling device driver.
235 * The device drivers which utilize the `kbdio' module are assumed to
236 * have the following set of routines.
237 * a. An interrupt handler (the bottom half of the driver).
238 * b. Timeout routines which may briefly poll the keyboard controller.
239 * c. Routines outside interrupt context (the top half of the driver).
240 * They should follow the rules below:
241 * 1. The interrupt handler may assume that it always has full access
242 * to the controller/keyboard/mouse.
243 * 2. The other routines must issue `spltty()' if they wish to
244 * prevent the interrupt handler from accessing
245 * the controller/keyboard/mouse.
246 * 3. The timeout routines and the top half routines of the device driver
247 * arbitrate I/O access by observing the lock flag in `kbdio'.
248 * The flag is manipulated via `kbdc_lock()'; when one wants to
249 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if
250 * the call returns with TRUE. Otherwise the caller must back off.
251 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion
252 * is finished. This mechanism does not prevent the interrupt
253 * handler from being invoked at any time and carrying out I/O.
254 * Therefore, `spltty()' must be strategically placed in the device
255 * driver code. Also note that the timeout routine may interrupt
256 * `kbdc_lock()' called by the top half of the driver, but this
257 * interruption is OK so long as the timeout routine observes
259 * 4. The interrupt and timeout routines should not extend I/O operation
260 * across more than one interrupt or timeout; they must complete any
261 * necessary I/O operation within one invocation of the routine.
262 * This means that if the timeout routine acquires the lock flag,
263 * it must reset the flag to FALSE before it returns.
266 /* set/reset polling lock */
268 kbdc_lock(KBDC p
, int lock
)
272 prevlock
= kbdcp(p
)->lock
;
273 kbdcp(p
)->lock
= lock
;
275 return (prevlock
!= lock
);
278 /* check if any data is waiting to be processed */
280 kbdc_data_ready(KBDC p
)
282 return (availq(&kbdcp(p
)->kbd
) || availq(&kbdcp(p
)->aux
)
283 || (read_status(kbdcp(p
)) & KBDS_ANY_BUFFER_FULL
));
286 /* queuing functions */
289 addq(kbdkqueue
*q
, int c
)
291 if (nextq(q
->tail
) != q
->head
) {
293 q
->tail
= nextq(q
->tail
);
297 if (q
->qcount
> q
->max_qcount
)
298 q
->max_qcount
= q
->qcount
;
306 removeq(kbdkqueue
*q
)
310 if (q
->tail
!= q
->head
) {
312 q
->head
= nextq(q
->head
);
322 * device I/O routines
325 wait_while_controller_busy(struct atkbdc_softc
*kbdc
)
327 /* CPU will stay inside the loop for 100msec at most */
331 while ((f
= read_status(kbdc
)) & KBDS_INPUT_BUFFER_FULL
) {
332 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
333 DELAY(KBDD_DELAYTIME
);
334 addq(&kbdc
->kbd
, read_data(kbdc
));
335 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
336 DELAY(KBDD_DELAYTIME
);
337 addq(&kbdc
->aux
, read_data(kbdc
));
339 DELAY(KBDC_DELAYTIME
);
347 * wait for any data; whether it's from the controller,
348 * the keyboard, or the aux device.
351 wait_for_data(struct atkbdc_softc
*kbdc
)
353 /* CPU will stay inside the loop for 200msec at most */
357 while ((f
= read_status(kbdc
) & KBDS_ANY_BUFFER_FULL
) == 0) {
358 DELAY(KBDC_DELAYTIME
);
362 DELAY(KBDD_DELAYTIME
);
366 /* wait for data from the keyboard */
368 wait_for_kbd_data(struct atkbdc_softc
*kbdc
)
370 /* CPU will stay inside the loop for 200msec at most */
374 while ((f
= read_status(kbdc
) & KBDS_BUFFER_FULL
)
375 != KBDS_KBD_BUFFER_FULL
) {
376 if (f
== KBDS_AUX_BUFFER_FULL
) {
377 DELAY(KBDD_DELAYTIME
);
378 addq(&kbdc
->aux
, read_data(kbdc
));
380 DELAY(KBDC_DELAYTIME
);
384 DELAY(KBDD_DELAYTIME
);
389 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard.
390 * queue anything else.
393 wait_for_kbd_ack(struct atkbdc_softc
*kbdc
)
395 /* CPU will stay inside the loop for 200msec at most */
400 while (retry
-- > 0) {
401 if ((f
= read_status(kbdc
)) & KBDS_ANY_BUFFER_FULL
) {
402 DELAY(KBDD_DELAYTIME
);
404 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
405 if ((b
== KBD_ACK
) || (b
== KBD_RESEND
)
406 || (b
== KBD_RESET_FAIL
))
409 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
413 DELAY(KBDC_DELAYTIME
);
418 /* wait for data from the aux device */
420 wait_for_aux_data(struct atkbdc_softc
*kbdc
)
422 /* CPU will stay inside the loop for 200msec at most */
426 while ((f
= read_status(kbdc
) & KBDS_BUFFER_FULL
)
427 != KBDS_AUX_BUFFER_FULL
) {
428 if (f
== KBDS_KBD_BUFFER_FULL
) {
429 DELAY(KBDD_DELAYTIME
);
430 addq(&kbdc
->kbd
, read_data(kbdc
));
432 DELAY(KBDC_DELAYTIME
);
436 DELAY(KBDD_DELAYTIME
);
441 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device.
442 * queue anything else.
445 wait_for_aux_ack(struct atkbdc_softc
*kbdc
)
447 /* CPU will stay inside the loop for 200msec at most */
452 while (retry
-- > 0) {
453 if ((f
= read_status(kbdc
)) & KBDS_ANY_BUFFER_FULL
) {
454 DELAY(KBDD_DELAYTIME
);
456 if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
457 if ((b
== PSM_ACK
) || (b
== PSM_RESEND
)
458 || (b
== PSM_RESET_FAIL
))
461 } else if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
465 DELAY(KBDC_DELAYTIME
);
470 /* write a one byte command to the controller */
472 write_controller_command(KBDC p
, int c
)
474 if (!wait_while_controller_busy(kbdcp(p
)))
476 write_command(kbdcp(p
), c
);
480 /* write a one byte data to the controller */
482 write_controller_data(KBDC p
, int c
)
484 if (!wait_while_controller_busy(kbdcp(p
)))
486 write_data(kbdcp(p
), c
);
490 /* write a one byte keyboard command */
492 write_kbd_command(KBDC p
, int c
)
494 if (!wait_while_controller_busy(kbdcp(p
)))
496 write_data(kbdcp(p
), c
);
500 /* write a one byte auxiliary device command */
502 write_aux_command(KBDC p
, int c
)
504 if (!write_controller_command(p
, KBDC_WRITE_TO_AUX
))
506 return write_controller_data(p
, c
);
509 /* send a command to the keyboard and wait for ACK */
511 send_kbd_command(KBDC p
, int c
)
513 int retry
= KBD_MAXRETRY
;
516 while (retry
-- > 0) {
517 if (!write_kbd_command(p
, c
))
519 res
= wait_for_kbd_ack(kbdcp(p
));
526 /* send a command to the auxiliary device and wait for ACK */
528 send_aux_command(KBDC p
, int c
)
530 int retry
= KBD_MAXRETRY
;
533 while (retry
-- > 0) {
534 if (!write_aux_command(p
, c
))
538 * The aux device may have already sent one or two bytes of
539 * status data, when a command is received. It will immediately
540 * stop data transmission, thus, leaving an incomplete data
541 * packet in our buffer. We have to discard any unprocessed
542 * data in order to remove such packets. Well, we may remove
543 * unprocessed, but necessary data byte as well...
545 emptyq(&kbdcp(p
)->aux
);
546 res
= wait_for_aux_ack(kbdcp(p
));
553 /* send a command and a data to the keyboard, wait for ACKs */
555 send_kbd_command_and_data(KBDC p
, int c
, int d
)
560 for (retry
= KBD_MAXRETRY
; retry
> 0; --retry
) {
561 if (!write_kbd_command(p
, c
))
563 res
= wait_for_kbd_ack(kbdcp(p
));
566 else if (res
!= KBD_RESEND
)
572 for (retry
= KBD_MAXRETRY
, res
= -1; retry
> 0; --retry
) {
573 if (!write_kbd_command(p
, d
))
575 res
= wait_for_kbd_ack(kbdcp(p
));
576 if (res
!= KBD_RESEND
)
582 /* send a command and a data to the auxiliary device, wait for ACKs */
584 send_aux_command_and_data(KBDC p
, int c
, int d
)
589 for (retry
= KBD_MAXRETRY
; retry
> 0; --retry
) {
590 if (!write_aux_command(p
, c
))
592 emptyq(&kbdcp(p
)->aux
);
593 res
= wait_for_aux_ack(kbdcp(p
));
596 else if (res
!= PSM_RESEND
)
602 for (retry
= KBD_MAXRETRY
, res
= -1; retry
> 0; --retry
) {
603 if (!write_aux_command(p
, d
))
605 res
= wait_for_aux_ack(kbdcp(p
));
606 if (res
!= PSM_RESEND
)
613 * read one byte from any source; whether from the controller,
614 * the keyboard, or the aux device
617 read_controller_data(KBDC p
)
619 if (availq(&kbdcp(p
)->kbd
))
620 return removeq(&kbdcp(p
)->kbd
);
621 if (availq(&kbdcp(p
)->aux
))
622 return removeq(&kbdcp(p
)->aux
);
623 if (!wait_for_data(kbdcp(p
)))
624 return -1; /* timeout */
625 return read_data(kbdcp(p
));
632 /* read one byte from the keyboard */
634 read_kbd_data(KBDC p
)
639 log(LOG_DEBUG
, "kbdc: kbd q: %d calls, max %d chars, "
640 "aux q: %d calls, max %d chars\n",
641 kbdcp(p
)->kbd
.call_count
, kbdcp(p
)->kbd
.max_qcount
,
642 kbdcp(p
)->aux
.call_count
, kbdcp(p
)->aux
.max_qcount
);
646 if (availq(&kbdcp(p
)->kbd
))
647 return removeq(&kbdcp(p
)->kbd
);
648 if (!wait_for_kbd_data(kbdcp(p
)))
649 return -1; /* timeout */
650 return read_data(kbdcp(p
));
653 /* read one byte from the keyboard, but return immediately if
657 read_kbd_data_no_wait(KBDC p
)
664 log(LOG_DEBUG
, "kbdc: kbd q: %d calls, max %d chars, "
665 "aux q: %d calls, max %d chars\n",
666 kbdcp(p
)->kbd
.call_count
, kbdcp(p
)->kbd
.max_qcount
,
667 kbdcp(p
)->aux
.call_count
, kbdcp(p
)->aux
.max_qcount
);
671 if (availq(&kbdcp(p
)->kbd
))
672 return removeq(&kbdcp(p
)->kbd
);
673 f
= read_status(kbdcp(p
)) & KBDS_BUFFER_FULL
;
674 if (f
== KBDS_AUX_BUFFER_FULL
) {
675 DELAY(KBDD_DELAYTIME
);
676 addq(&kbdcp(p
)->aux
, read_data(kbdcp(p
)));
677 f
= read_status(kbdcp(p
)) & KBDS_BUFFER_FULL
;
679 if (f
== KBDS_KBD_BUFFER_FULL
) {
680 DELAY(KBDD_DELAYTIME
);
681 return read_data(kbdcp(p
));
683 return -1; /* no data */
686 /* read one byte from the aux device */
688 read_aux_data(KBDC p
)
690 if (availq(&kbdcp(p
)->aux
))
691 return removeq(&kbdcp(p
)->aux
);
692 if (!wait_for_aux_data(kbdcp(p
)))
693 return -1; /* timeout */
694 return read_data(kbdcp(p
));
697 /* read one byte from the aux device, but return immediately if
701 read_aux_data_no_wait(KBDC p
)
705 if (availq(&kbdcp(p
)->aux
))
706 return removeq(&kbdcp(p
)->aux
);
707 f
= read_status(kbdcp(p
)) & KBDS_BUFFER_FULL
;
708 if (f
== KBDS_KBD_BUFFER_FULL
) {
709 DELAY(KBDD_DELAYTIME
);
710 addq(&kbdcp(p
)->kbd
, read_data(kbdcp(p
)));
711 f
= read_status(kbdcp(p
)) & KBDS_BUFFER_FULL
;
713 if (f
== KBDS_AUX_BUFFER_FULL
) {
714 DELAY(KBDD_DELAYTIME
);
715 return read_data(kbdcp(p
));
717 return -1; /* no data */
720 /* discard data from the keyboard */
722 empty_kbd_buffer(KBDC p
, int wait
)
733 for (t
= wait
; t
> 0; ) {
734 if ((f
= read_status(kbdcp(p
))) & KBDS_ANY_BUFFER_FULL
) {
735 DELAY(KBDD_DELAYTIME
);
736 b
= read_data(kbdcp(p
));
737 if ((f
& KBDS_BUFFER_FULL
) == KBDS_AUX_BUFFER_FULL
) {
738 addq(&kbdcp(p
)->aux
, b
);
752 if ((c1
> 0) || (c2
> 0))
753 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1
, c2
);
756 emptyq(&kbdcp(p
)->kbd
);
759 /* discard data from the aux device */
761 empty_aux_buffer(KBDC p
, int wait
)
772 for (t
= wait
; t
> 0; ) {
773 if ((f
= read_status(kbdcp(p
))) & KBDS_ANY_BUFFER_FULL
) {
774 DELAY(KBDD_DELAYTIME
);
775 b
= read_data(kbdcp(p
));
776 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
) {
777 addq(&kbdcp(p
)->kbd
, b
);
791 if ((c1
> 0) || (c2
> 0))
792 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1
, c2
);
795 emptyq(&kbdcp(p
)->aux
);
798 /* discard any data from the keyboard or the aux device */
800 empty_both_buffers(KBDC p
, int wait
)
810 for (t
= wait
; t
> 0; ) {
811 if ((f
= read_status(kbdcp(p
))) & KBDS_ANY_BUFFER_FULL
) {
812 DELAY(KBDD_DELAYTIME
);
813 (void)read_data(kbdcp(p
));
815 if ((f
& KBDS_BUFFER_FULL
) == KBDS_KBD_BUFFER_FULL
)
827 if ((c1
> 0) || (c2
> 0))
828 log(LOG_DEBUG
, "kbdc: %d:%d char read (empty_both_buffers)\n", c1
, c2
);
831 emptyq(&kbdcp(p
)->kbd
);
832 emptyq(&kbdcp(p
)->aux
);
835 /* keyboard and mouse device control */
837 /* NOTE: enable the keyboard port but disable the keyboard
838 * interrupt before calling "reset_kbd()".
843 int retry
= KBD_MAXRETRY
;
844 int again
= KBD_MAXWAIT
;
845 int c
= KBD_RESEND
; /* keep the compiler happy */
847 while (retry
-- > 0) {
848 empty_both_buffers(p
, 10);
849 if (!write_kbd_command(p
, KBDC_RESET_KBD
))
851 emptyq(&kbdcp(p
)->kbd
);
852 c
= read_controller_data(p
);
853 if (verbose
|| bootverbose
)
854 log(LOG_DEBUG
, "kbdc: RESET_KBD return code:%04x\n", c
);
855 if (c
== KBD_ACK
) /* keyboard has agreed to reset itself... */
861 while (again
-- > 0) {
862 /* wait awhile, well, in fact we must wait quite loooooooooooong */
863 DELAY(KBD_RESETDELAY
*1000);
864 c
= read_controller_data(p
); /* RESET_DONE/RESET_FAIL */
865 if (c
!= -1) /* wait again if the controller is not ready */
868 if (verbose
|| bootverbose
)
869 log(LOG_DEBUG
, "kbdc: RESET_KBD status:%04x\n", c
);
870 if (c
!= KBD_RESET_DONE
)
875 /* NOTE: enable the aux port but disable the aux interrupt
876 * before calling `reset_aux_dev()'.
879 reset_aux_dev(KBDC p
)
881 int retry
= KBD_MAXRETRY
;
882 int again
= KBD_MAXWAIT
;
883 int c
= PSM_RESEND
; /* keep the compiler happy */
885 while (retry
-- > 0) {
886 empty_both_buffers(p
, 10);
887 if (!write_aux_command(p
, PSMC_RESET_DEV
))
889 emptyq(&kbdcp(p
)->aux
);
890 /* NOTE: Compaq Armada laptops require extra delay here. XXX */
891 for (again
= KBD_MAXWAIT
; again
> 0; --again
) {
892 DELAY(KBD_RESETDELAY
*1000);
893 c
= read_aux_data_no_wait(p
);
897 if (verbose
|| bootverbose
)
898 log(LOG_DEBUG
, "kbdc: RESET_AUX return code:%04x\n", c
);
899 if (c
== PSM_ACK
) /* aux dev is about to reset... */
905 for (again
= KBD_MAXWAIT
; again
> 0; --again
) {
906 /* wait awhile, well, quite looooooooooooong */
907 DELAY(KBD_RESETDELAY
*1000);
908 c
= read_aux_data_no_wait(p
); /* RESET_DONE/RESET_FAIL */
909 if (c
!= -1) /* wait again if the controller is not ready */
912 if (verbose
|| bootverbose
)
913 log(LOG_DEBUG
, "kbdc: RESET_AUX status:%04x\n", c
);
914 if (c
!= PSM_RESET_DONE
) /* reset status */
917 c
= read_aux_data(p
); /* device ID */
918 if (verbose
|| bootverbose
)
919 log(LOG_DEBUG
, "kbdc: RESET_AUX ID:%04x\n", c
);
920 /* NOTE: we could check the device ID now, but leave it later... */
924 /* controller diagnostics and setup */
927 test_controller(KBDC p
)
929 int retry
= KBD_MAXRETRY
;
930 int again
= KBD_MAXWAIT
;
931 int c
= KBD_DIAG_FAIL
;
933 while (retry
-- > 0) {
934 empty_both_buffers(p
, 10);
935 if (write_controller_command(p
, KBDC_DIAGNOSE
))
941 emptyq(&kbdcp(p
)->kbd
);
942 while (again
-- > 0) {
944 DELAY(KBD_RESETDELAY
*1000);
945 c
= read_controller_data(p
); /* DIAG_DONE/DIAG_FAIL */
946 if (c
!= -1) /* wait again if the controller is not ready */
949 if (verbose
|| bootverbose
)
950 log(LOG_DEBUG
, "kbdc: DIAGNOSE status:%04x\n", c
);
951 return (c
== KBD_DIAG_DONE
);
955 test_kbd_port(KBDC p
)
957 int retry
= KBD_MAXRETRY
;
958 int again
= KBD_MAXWAIT
;
961 while (retry
-- > 0) {
962 empty_both_buffers(p
, 10);
963 if (write_controller_command(p
, KBDC_TEST_KBD_PORT
))
969 emptyq(&kbdcp(p
)->kbd
);
970 while (again
-- > 0) {
971 c
= read_controller_data(p
);
972 if (c
!= -1) /* try again if the controller is not ready */
975 if (verbose
|| bootverbose
)
976 log(LOG_DEBUG
, "kbdc: TEST_KBD_PORT status:%04x\n", c
);
981 test_aux_port(KBDC p
)
983 int retry
= KBD_MAXRETRY
;
984 int again
= KBD_MAXWAIT
;
987 while (retry
-- > 0) {
988 empty_both_buffers(p
, 10);
989 if (write_controller_command(p
, KBDC_TEST_AUX_PORT
))
995 emptyq(&kbdcp(p
)->kbd
);
996 while (again
-- > 0) {
997 c
= read_controller_data(p
);
998 if (c
!= -1) /* try again if the controller is not ready */
1001 if (verbose
|| bootverbose
)
1002 log(LOG_DEBUG
, "kbdc: TEST_AUX_PORT status:%04x\n", c
);
1007 kbdc_get_device_mask(KBDC p
)
1009 return kbdcp(p
)->command_mask
;
1013 kbdc_set_device_mask(KBDC p
, int mask
)
1015 kbdcp(p
)->command_mask
=
1016 mask
& (KBD_KBD_CONTROL_BITS
| KBD_AUX_CONTROL_BITS
);
1020 get_controller_command_byte(KBDC p
)
1022 if (kbdcp(p
)->command_byte
!= -1)
1023 return kbdcp(p
)->command_byte
;
1024 if (!write_controller_command(p
, KBDC_GET_COMMAND_BYTE
))
1026 emptyq(&kbdcp(p
)->kbd
);
1027 kbdcp(p
)->command_byte
= read_controller_data(p
);
1028 return kbdcp(p
)->command_byte
;
1032 set_controller_command_byte(KBDC p
, int mask
, int command
)
1034 if (get_controller_command_byte(p
) == -1)
1037 command
= (kbdcp(p
)->command_byte
& ~mask
) | (command
& mask
);
1038 if (command
& KBD_DISABLE_KBD_PORT
) {
1039 if (!write_controller_command(p
, KBDC_DISABLE_KBD_PORT
))
1042 if (!write_controller_command(p
, KBDC_SET_COMMAND_BYTE
))
1044 if (!write_controller_data(p
, command
))
1046 kbdcp(p
)->command_byte
= command
;
1049 log(LOG_DEBUG
, "kbdc: new command byte:%04x (set_controller...)\n",