1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
/*-
* Copyright (c) 1996 Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: kbdio.c,v 1.1 1996/11/14 22:19:06 sos Exp $
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/syslog.h>
#include <machine/clock.h>
#include <i386/isa/isa.h>
#include <i386/isa/isa_device.h>
#include <i386/isa/kbdio.h>
#ifndef KBDIO_DEBUG
#define KBDIO_DEBUG 0
#endif
static int verbose = KBDIO_DEBUG;
/*
* device I/O routines
*/
int
wait_while_controller_busy(int port)
{
/* CPU will stay inside the loop for 100msec at most */
int retry = 5000;
while (inb(port + KBD_STATUS_PORT) & KBDS_INPUT_BUFFER_FULL) {
DELAY(20);
if (--retry < 0)
return FALSE;
}
return TRUE;
}
/*
* wait for any data; whether it's from the controller,
* the keyboard, or the aux device.
*/
int
wait_for_data(int port)
{
/* CPU will stay inside the loop for 200msec at most */
int retry = 10000;
while ((inb(port + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) == 0) {
DELAY(20);
if (--retry < 0)
return FALSE;
}
DELAY(7);
return TRUE;
}
/* wait for data from the keyboard */
int
wait_for_kbd_data(int port)
{
/* CPU will stay inside the loop for 200msec at most */
int retry = 10000;
while ((inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
!= KBDS_KBD_BUFFER_FULL) {
DELAY(20);
if (--retry < 0)
return FALSE;
}
DELAY(7);
return TRUE;
}
/* wait for data from the aux device */
int
wait_for_aux_data(int port)
{
/* CPU will stay inside the loop for 200msec at most */
int retry = 10000;
while ((inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
!= KBDS_AUX_BUFFER_FULL) {
DELAY(20);
if (--retry < 0)
return FALSE;
}
DELAY(7);
return TRUE;
}
int
write_controller_command(int port, int c)
{
if (!wait_while_controller_busy(port))
return FALSE;
outb(port + KBD_COMMAND_PORT, c);
return TRUE;
}
int
write_controller_data(int port, int c)
{
if (!wait_while_controller_busy(port))
return FALSE;
outb(port + KBD_DATA_PORT, c);
return TRUE;
}
int
write_kbd_command(int port, int c)
{
if (!wait_while_controller_busy(port))
return FALSE;
outb(port + KBD_DATA_PORT, c);
return TRUE;
}
int
write_aux_command(int port, int c)
{
if (!write_controller_command(port,KBDC_WRITE_TO_AUX))
return FALSE;
return write_controller_data(port, c);
}
int
send_kbd_command(int port, int c)
{
int retry = KBD_MAXRETRY;
int res = -1;
while (retry-- > 0) {
if (!write_kbd_command(port, c))
continue;
res = read_controller_data(port);
if (res == KBD_ACK)
break;
}
return res;
}
int
send_aux_command(int port, int c)
{
int retry = KBD_MAXRETRY;
int res = -1;
while (retry-- > 0) {
if (!write_aux_command(port, c))
continue;
res = read_aux_data(port);
if (res == PSM_ACK)
break;
}
return res;
}
int
send_kbd_command_and_data(int port, int c, int d)
{
int retry;
int res = -1;
for (retry = KBD_MAXRETRY; retry > 0; --retry) {
if (!write_kbd_command(port, c))
continue;
res = read_controller_data(port);
if (res == KBD_ACK)
break;
else if (res != PSM_RESEND)
return res;
}
if (retry <= 0)
return res;
for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
if (!write_kbd_command(port, d))
continue;
res = read_controller_data(port);
if (res != KBD_RESEND)
break;
}
return res;
}
int
send_aux_command_and_data(int port, int c, int d)
{
int retry;
int res = -1;
for (retry = KBD_MAXRETRY; retry > 0; --retry) {
if (!write_aux_command(port, c))
continue;
res = read_aux_data(port);
if (res == PSM_ACK)
break;
else if (res != PSM_RESEND)
return res;
}
if (retry <= 0)
return res;
for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) {
if (!write_aux_command(port, d))
continue;
res = read_aux_data(port);
if (res != PSM_RESEND)
break;
}
return res;
}
/*
* read one byte from any source; whether from the controller,
* the keyboard, or the aux device
*/
int
read_controller_data(int port)
{
if (!wait_for_data(port))
return -1; /* timeout */
return inb(port + KBD_DATA_PORT);
}
/* read one byte from the keyboard */
int
read_kbd_data(int port)
{
if (!wait_for_kbd_data(port))
return -1; /* timeout */
return inb(port + KBD_DATA_PORT);
}
/* read one byte from the keyboard, but return immediately if
* no data is waiting
*/
int
read_kbd_data_no_wait(int port)
{
if ((inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
!= KBDS_KBD_BUFFER_FULL)
return -1; /* no data */
DELAY(7);
return inb(port + KBD_DATA_PORT);
}
/* read one byte from the aux device */
int
read_aux_data(int port)
{
if (!wait_for_aux_data(port))
return -1; /* timeout */
return inb(port + KBD_DATA_PORT);
}
/* discard data from the keyboard */
void
empty_kbd_buffer(int port, int t)
{
int b;
int c = 0;
int delta = 2;
for (; t > 0; t -= delta) {
if ((inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
== KBDS_KBD_BUFFER_FULL) {
DELAY(7);
b = inb(port + KBD_DATA_PORT);
++c;
}
DELAY(delta*1000);
}
if ((verbose >= 2) && (c > 0))
log(LOG_DEBUG,"kbdio: %d char read (empty_kbd_buffer)\n",c);
}
/* discard data from the aux device */
void
empty_aux_buffer(int port, int t)
{
int b;
int c = 0;
int delta = 2;
for (; t > 0; t -= delta) {
if ((inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)
== KBDS_AUX_BUFFER_FULL) {
DELAY(7);
b = inb(port + KBD_DATA_PORT);
++c;
}
DELAY(delta*1000);
}
if ((verbose >= 2) && (c > 0))
log(LOG_DEBUG,"kbdio: %d char read (empty_aux_buffer)\n",c);
}
/* discard any data from the keyboard or the aux device */
void
empty_both_buffers(int port, int t)
{
int b;
int c = 0;
int delta = 2;
for (; t > 0; t -= delta) {
if (inb(port + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) {
DELAY(7);
b = inb(port + KBD_DATA_PORT);
++c;
}
DELAY(delta*1000);
}
if ((verbose >= 2) && (c > 0))
log(LOG_DEBUG,"kbdio: %d char read (empty_both_buffers)\n",c);
}
/* keyboard and mouse device control */
/* NOTE: enable the keyboard port but disable the keyboard
* interrupt before calling "reset_kbd()".
*/
int
reset_kbd(int port)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = KBD_RESEND; /* keep the compiler happy */
while (retry-- > 0) {
empty_both_buffers(port, 10);
if (!write_kbd_command(port, KBDC_RESET_KBD))
continue;
c = read_controller_data(port);
if (verbose)
log(LOG_DEBUG,"kbdio: RESET_KBD return code:%04x\n",c);
if (c == KBD_ACK) /* keyboard has agreed to reset itself... */
break;
}
if (retry < 0)
return FALSE;
while (again-- > 0) {
/* wait awhile, well, in fact we must wait quite loooooooooooong */
DELAY(KBD_RESETDELAY*1000);
c = read_controller_data(port); /* RESET_DONE/RESET_FAIL */
if (c != -1) /* wait again if the controller is not ready */
break;
}
if (verbose)
log(LOG_DEBUG,"kbdio: RESET_KBD status:%04x\n",c);
if (c != KBD_RESET_DONE)
return FALSE;
return TRUE;
}
/* NOTE: enable the aux port but disable the aux interrupt
* before calling `reset_aux_dev()'.
*/
int
reset_aux_dev(int port)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = PSM_RESEND; /* keep the compiler happy */
while (retry-- > 0) {
empty_both_buffers(port, 10);
if (!write_aux_command(port, PSMC_RESET_DEV))
continue;
c = read_controller_data(port);
if (verbose)
log(LOG_DEBUG,"kbdio: RESET_AUX return code:%04x\n",c);
if (c == PSM_ACK) /* aux dev is about to reset... */
break;
}
if (retry < 0)
return FALSE;
while (again-- > 0) {
/* wait awhile, well, quite looooooooooooong */
DELAY(KBD_RESETDELAY*1000);
c = read_aux_data(port); /* RESET_DONE/RESET_FAIL */
if (c != -1) /* wait again if the controller is not ready */
break;
}
if (verbose)
log(LOG_DEBUG,"kbdio: RESET_AUX status:%04x\n",c);
if (c != PSM_RESET_DONE) /* reset status */
return FALSE;
c = read_aux_data(port); /* device ID */
if (verbose)
log(LOG_DEBUG,"kbdio: RESET_AUX ID:%04x\n",c);
/* NOTE: we could check the device ID now, but leave it later... */
return TRUE;
}
/* controller diagnostics and setup */
int
test_controller(int port)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = KBD_DIAG_FAIL;
while (retry-- > 0) {
empty_both_buffers(port, 10);
if (write_controller_command(port, KBDC_DIAGNOSE))
break;
}
if (retry < 0)
return FALSE;
while (again-- > 0) {
/* wait awhile */
DELAY(KBD_RESETDELAY*1000);
c = read_controller_data(port); /* DIAG_DONE/DIAG_FAIL */
if (c != -1) /* wait again if the controller is not ready */
break;
}
if (verbose)
log(LOG_DEBUG,"kbdio: DIAGNOSE status:%04x\n",c);
return (c == KBD_DIAG_DONE);
}
int
test_kbd_port(int port)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = -1;
while (retry-- > 0) {
empty_both_buffers(port, 10);
if (write_controller_command(port, KBDC_TEST_KBD_PORT))
break;
}
if (retry < 0)
return FALSE;
while (again-- > 0) {
c = read_controller_data(port);
if (c != -1) /* try again if the controller is not ready */
break;
}
if (verbose)
log(LOG_DEBUG,"kbdio: TEST_KBD_PORT status:%04x\n",c);
return c;
}
int
test_aux_port(int port)
{
int retry = KBD_MAXRETRY;
int again = KBD_MAXWAIT;
int c = -1;
while (retry-- > 0) {
empty_both_buffers(port, 10);
if (write_controller_command(port, KBDC_TEST_AUX_PORT))
break;
}
if (retry < 0)
return FALSE;
while (again-- > 0) {
c = read_controller_data(port);
if (c != -1) /* try again if the controller is not ready */
break;
}
if (verbose)
log(LOG_DEBUG,"kbdio: TEST_AUX_PORT status:%04x\n",c);
return c;
}
int
set_controller_command_byte(int port, int command, int flag)
{
if ((command | flag) & KBD_DISABLE_KBD_PORT) {
if (!write_controller_command(port, KBDC_DISABLE_KBD_PORT))
return FALSE;
}
if (!write_controller_command(port, KBDC_SET_COMMAND_BYTE))
return FALSE;
if (!write_controller_data(port, command | flag))
return FALSE;
wait_while_controller_busy(port);
return TRUE;
}
|