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