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