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