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