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 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bus.h> 41 #include <sys/kbio.h> 42 #include <sys/kernel.h> 43 #include <sys/malloc.h> 44 #include <sys/rman.h> 45 #include <sys/syslog.h> 46 47 #include <machine/clock.h> 48 49 #include <bus/isa/isareg.h> 50 51 #include "atkbdcreg.h" 52 #include "kbdreg.h" 53 #include "use_atkbdc.h" 54 55 /* constants */ 56 57 #define MAXKBDC MAX(NATKBDC, 1) /* XXX */ 58 59 /* macros */ 60 61 #ifndef MAX 62 #define MAX(x, y) ((x) > (y) ? (x) : (y)) 63 #endif 64 65 #define nextq(i) (((i) + 1) % KBDQ_BUFSIZE) 66 #define availq(q) ((q)->head != (q)->tail) 67 #if KBDIO_DEBUG >= 2 68 #define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0) 69 #else 70 #define emptyq(q) ((q)->tail = (q)->head = 0) 71 #endif 72 73 #define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0)) 74 #define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0)) 75 #define write_data(k, d) (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d))) 76 #define write_command(k, d) (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d))) 77 78 /* local variables */ 79 80 /* 81 * We always need at least one copy of the kbdc_softc struct for the 82 * low-level console. As the low-level console accesses the keyboard 83 * controller before kbdc, and all other devices, is probed, we 84 * statically allocate one entry. XXX 85 */ 86 static atkbdc_softc_t default_kbdc; 87 static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc }; 88 89 static int verbose = KBDIO_DEBUG; 90 91 /* function prototypes */ 92 93 static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, 94 bus_space_handle_t h0, bus_space_handle_t h1); 95 static int addq(kbdkqueue *q, int c); 96 static int removeq(kbdkqueue *q); 97 static int wait_while_controller_busy(atkbdc_softc_t *kbdc); 98 static int wait_for_data(atkbdc_softc_t *kbdc); 99 static int wait_for_kbd_data(atkbdc_softc_t *kbdc); 100 static int wait_for_kbd_ack(atkbdc_softc_t *kbdc); 101 static int wait_for_aux_data(atkbdc_softc_t *kbdc); 102 static int wait_for_aux_ack(atkbdc_softc_t *kbdc); 103 104 struct atkbdc_quirks { 105 const char *bios_vendor; 106 const char *maker; 107 const char *product; 108 const char *version; 109 int quirk; 110 }; 111 112 /* Old chromebooks running coreboot with i8042 emulation quirks */ 113 #define CHROMEBOOK_WORKAROUND \ 114 (KBDC_QUIRK_KEEP_ACTIVATED | KBDC_QUIRK_IGNORE_PROBE_RESULT | \ 115 KBDC_QUIRK_RESET_AFTER_PROBE | KBDC_QUIRK_SETLEDS_ON_INIT) 116 117 static struct atkbdc_quirks quirks[] = { 118 /* 119 * Older chromebooks running coreboot have an EC that imperfectly 120 * emulates i8042 w/o fixes to its firmware. Since we can't probe for 121 * the problem, include all chromebooks by matching 'Google_' in the 122 * bios version string or a maker of either 'Google' or 'GOOGLE'. This 123 * is imperfect, but catches all chromebooks while omitting non-Google 124 * systems from System76 and Purism. 125 */ 126 { "coreboot", NULL, NULL, "Google_", CHROMEBOOK_WORKAROUND }, 127 { "coreboot", "GOOGLE", NULL, NULL, CHROMEBOOK_WORKAROUND }, 128 { "coreboot", "Google", NULL, NULL, CHROMEBOOK_WORKAROUND }, 129 /* KBDC hangs on Lenovo X120e and X121e after disabling AUX MUX */ 130 { NULL, "LENOVO", NULL, NULL, KBDC_QUIRK_DISABLE_MUX_PROBE }, 131 }; 132 133 #define QUIRK_STR_EQUAL(s1, s2) \ 134 (s1 == NULL || (s2 != NULL && strcmp(s1, s2) == 0)) 135 #define QUIRK_STR_MATCH(s1, s2) \ 136 (s1 == NULL || (s2 != NULL && strncmp(s1, s2, strlen(s1)) == 0)) 137 138 static int 139 atkbdc_getquirks(void) 140 { 141 int i; 142 char *bios_vendor = kgetenv("smbios.bios.vendor"); 143 char *maker = kgetenv("smbios.system.maker"); 144 char *product = kgetenv("smbios.system.product"); 145 char *version = kgetenv("smbios.bios.version"); 146 char *reldate = kgetenv("smbios.bios.reldate"); 147 148 for (i = 0; i < nitems(quirks); i++) 149 if (QUIRK_STR_EQUAL(quirks[i].bios_vendor, bios_vendor) && 150 QUIRK_STR_EQUAL(quirks[i].maker, maker) && 151 QUIRK_STR_EQUAL(quirks[i].product, product) && 152 QUIRK_STR_MATCH(quirks[i].version, version)) 153 return (quirks[i].quirk); 154 /* 155 * Some Chromebooks don't conform to the google comment above so do the 156 * Chromebook workaround for all <= 2018 coreboot systems that have a 157 * 'blank' version. At least one Acer "Peppy" chromebook has this 158 * issue, with a reldate of 08/13/2014. 159 */ 160 if (QUIRK_STR_EQUAL("coreboot", bios_vendor) && 161 (version != NULL && *version == ' ') && 162 (reldate != NULL && strlen(reldate) >= 10 && 163 strcmp(reldate + 6, "2018") <= 0)) 164 return (CHROMEBOOK_WORKAROUND); 165 166 return (0); 167 } 168 169 atkbdc_softc_t * 170 atkbdc_get_softc(int unit) 171 { 172 atkbdc_softc_t *sc; 173 174 if (unit >= nitems(atkbdc_softc)) 175 return NULL; 176 sc = atkbdc_softc[unit]; 177 if (sc == NULL) { 178 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO); 179 atkbdc_softc[unit] = sc; 180 } 181 return sc; 182 } 183 184 int 185 atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1) 186 { 187 if (rman_get_start(port0) <= 0) 188 return ENXIO; 189 if (rman_get_start(port1) <= 0) 190 return ENXIO; 191 return 0; 192 } 193 194 int 195 atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0, 196 struct resource *port1) 197 { 198 return atkbdc_setup(sc, rman_get_bustag(port0), 199 rman_get_bushandle(port0), rman_get_bushandle(port1)); 200 } 201 202 extern int acpi_fadt_8042_nolegacy; 203 int kicked_by_syscons = 0; 204 205 static void 206 atkbdc_fadt_done(void) 207 { 208 if (kicked_by_syscons) { 209 /* 210 * Configuring all keyboards is fine, because only atkbd 211 * really does something here, anyway. 212 */ 213 kbd_configure(KB_CONF_PROBE_ONLY); 214 } 215 } 216 217 /* After fadt_probe in platform/pc64/acpica/acpi_fadt.c. */ 218 SYSINIT(atkbdc_kick, SI_BOOT2_PRESMP, SI_ORDER_THIRD, atkbdc_fadt_done, 0); 219 220 /* the backdoor to the keyboard controller! XXX */ 221 int 222 atkbdc_configure(void) 223 { 224 bus_space_tag_t tag; 225 bus_space_handle_t h0; 226 bus_space_handle_t h1; 227 int port0; 228 int port1; 229 #if defined(__x86_64__) 230 int i; 231 #endif 232 233 kicked_by_syscons = 1; 234 if (acpi_fadt_8042_nolegacy != 0) 235 return ENXIO; 236 237 port0 = IO_KBD; 238 resource_int_value("atkbdc", 0, "port", &port0); 239 port1 = IO_KBD + KBD_STATUS_PORT; 240 #if 0 241 resource_int_value("atkbdc", 0, "port", &port0); 242 #endif 243 244 /* XXX: tag should be passed from the caller */ 245 #if defined(__x86_64__) 246 tag = X86_64_BUS_SPACE_IO; 247 #else 248 tag = 0; /* XXX */ 249 #endif 250 251 #if 0 /* notyet */ 252 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0); 253 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1); 254 #else 255 h0 = (bus_space_handle_t)port0; 256 h1 = (bus_space_handle_t)port1; 257 #endif 258 259 #if defined(__x86_64__) 260 /* 261 * Check if we really have AT keyboard controller. Poll status 262 * register until we get "all clear" indication. If no such 263 * indication comes, it probably means that there is no AT 264 * keyboard controller present. Give up in such case. Check relies 265 * on the fact that reading from non-existing in/out port returns 266 * 0xff on i386. May or may not be true on other platforms. 267 */ 268 for (i = 65536; i != 0; --i) { 269 if ((bus_space_read_1(tag, h1, 0) & 0x2) == 0) 270 break; 271 DELAY(16); 272 } 273 if (i == 0) 274 return ENXIO; 275 #endif 276 277 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1); 278 } 279 280 static int 281 atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0, 282 bus_space_handle_t h1) 283 { 284 u_int64_t tscval[3], read_delay; 285 register_t flags; 286 287 if (sc->ioh0 == 0) { /* XXX */ 288 sc->command_byte = -1; 289 sc->command_mask = 0; 290 sc->lock = FALSE; 291 sc->kbd.head = sc->kbd.tail = 0; 292 sc->aux.head = sc->aux.tail = 0; 293 sc->aux_mux_enabled = FALSE; 294 #if KBDIO_DEBUG >= 2 295 sc->kbd.call_count = 0; 296 sc->kbd.qcount = sc->kbd.max_qcount = 0; 297 sc->aux.call_count = 0; 298 sc->aux.qcount = sc->aux.max_qcount = 0; 299 #endif 300 } 301 sc->iot = tag; 302 sc->ioh0 = h0; 303 sc->ioh1 = h1; 304 /* 305 * On certain chipsets AT keyboard controller isn't present and is 306 * emulated by BIOS using SMI interrupt. On those chipsets reading 307 * from the status port may be thousand times slower than usually. 308 * Sometimes this emilation is not working properly resulting in 309 * commands timing our and since we assume that inb() operation 310 * takes very little time to complete we need to adjust number of 311 * retries to keep waiting time within a designed limits (100ms). 312 * Measure time it takes to make read_status() call and adjust 313 * number of retries accordingly. 314 */ 315 flags = intr_disable(); 316 tscval[0] = rdtsc(); 317 read_status(sc); 318 tscval[1] = rdtsc(); 319 DELAY(1000); 320 tscval[2] = rdtsc(); 321 intr_restore(flags); 322 read_delay = tscval[1] - tscval[0]; 323 read_delay /= (tscval[2] - tscval[1]) / 1000; 324 sc->retry = 100000 / ((KBDD_DELAYTIME * 2) + read_delay); 325 326 sc->quirks = atkbdc_getquirks(); 327 328 return 0; 329 } 330 331 /* open a keyboard controller */ 332 KBDC 333 atkbdc_open(int unit) 334 { 335 if (unit <= 0) 336 unit = 0; 337 if (unit >= MAXKBDC) 338 return NULL; 339 if ((atkbdc_softc[unit]->port0 != NULL) || 340 (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */ 341 return (KBDC)atkbdc_softc[unit]; 342 return NULL; 343 } 344 345 /* 346 * I/O access arbitration in `kbdio' 347 * 348 * The `kbdio' module uses a simplistic convention to arbitrate 349 * I/O access to the controller/keyboard/mouse. The convention requires 350 * close cooperation of the calling device driver. 351 * 352 * The device drivers which utilize the `kbdio' module are assumed to 353 * have the following set of routines. 354 * a. An interrupt handler (the bottom half of the driver). 355 * b. Timeout routines which may briefly poll the keyboard controller. 356 * c. Routines outside interrupt context (the top half of the driver). 357 * They should follow the rules below: 358 * 1. The interrupt handler may assume that it always has full access 359 * to the controller/keyboard/mouse. 360 * 2. The other routines must issue `spltty()' if they wish to 361 * prevent the interrupt handler from accessing 362 * the controller/keyboard/mouse. 363 * 3. The timeout routines and the top half routines of the device driver 364 * arbitrate I/O access by observing the lock flag in `kbdio'. 365 * The flag is manipulated via `kbdc_lock()'; when one wants to 366 * perform I/O, call `kbdc_lock(kbdc, TRUE)' and proceed only if 367 * the call returns with TRUE. Otherwise the caller must back off. 368 * Call `kbdc_lock(kbdc, FALSE)' when necessary I/O operaion 369 * is finished. This mechanism does not prevent the interrupt 370 * handler from being invoked at any time and carrying out I/O. 371 * Therefore, `spltty()' must be strategically placed in the device 372 * driver code. Also note that the timeout routine may interrupt 373 * `kbdc_lock()' called by the top half of the driver, but this 374 * interruption is OK so long as the timeout routine observes 375 * rule 4 below. 376 * 4. The interrupt and timeout routines should not extend I/O operation 377 * across more than one interrupt or timeout; they must complete any 378 * necessary I/O operation within one invocation of the routine. 379 * This means that if the timeout routine acquires the lock flag, 380 * it must reset the flag to FALSE before it returns. 381 */ 382 383 /* set/reset polling lock */ 384 int 385 kbdc_lock(KBDC p, int lock) 386 { 387 int prevlock; 388 389 prevlock = p->lock; 390 p->lock = lock; 391 392 return (prevlock != lock); 393 } 394 395 /* check if any data is waiting to be processed */ 396 int 397 kbdc_data_ready(KBDC p) 398 { 399 return (availq(&p->kbd) || availq(&p->aux) || 400 (read_status(p) & KBDS_ANY_BUFFER_FULL)); 401 } 402 403 /* queuing functions */ 404 405 static int 406 addq(kbdkqueue *q, int c) 407 { 408 if (nextq(q->tail) != q->head) { 409 q->q[q->tail] = c; 410 q->tail = nextq(q->tail); 411 #if KBDIO_DEBUG >= 2 412 ++q->call_count; 413 ++q->qcount; 414 if (q->qcount > q->max_qcount) 415 q->max_qcount = q->qcount; 416 #endif 417 return TRUE; 418 } 419 return FALSE; 420 } 421 422 static int 423 removeq(kbdkqueue *q) 424 { 425 int c; 426 427 if (q->tail != q->head) { 428 c = q->q[q->head]; 429 q->head = nextq(q->head); 430 #if KBDIO_DEBUG >= 2 431 --q->qcount; 432 #endif 433 return c; 434 } 435 return -1; 436 } 437 438 /* 439 * device I/O routines 440 */ 441 static int 442 wait_while_controller_busy(struct atkbdc_softc *kbdc) 443 { 444 /* CPU will stay inside the loop for 100msec at most */ 445 TOTALDELAY retry = { .us = 70000, .last_clock = 0 }; /* 70ms */ 446 int f; 447 unsigned char c; 448 449 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) { 450 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) { 451 DELAY(KBDD_DELAYTIME); 452 c = read_data(kbdc); 453 addq(&kbdc->kbd, c); 454 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) { 455 DELAY(KBDD_DELAYTIME); 456 c = read_data(kbdc); 457 addq(&kbdc->aux, c); 458 } 459 DELAY(KBDC_DELAYTIME); 460 if (CHECKTIMEOUT(&retry)) 461 return FALSE; 462 } 463 return TRUE; 464 } 465 466 /* 467 * wait for any data; whether it's from the controller, 468 * the keyboard, or the aux device. 469 */ 470 static int 471 wait_for_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 477 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) { 478 DELAY(KBDC_DELAYTIME); 479 if (CHECKTIMEOUT(&retry)) 480 return 0; 481 } 482 DELAY(KBDD_DELAYTIME); 483 return f; 484 } 485 486 /* wait for data from the keyboard */ 487 static int 488 wait_for_kbd_data(struct atkbdc_softc *kbdc) 489 { 490 /* CPU will stay inside the loop for 200msec at most */ 491 TOTALDELAY retry = { 200000, 0 }; /* 200ms */ 492 int f; 493 unsigned char c; 494 495 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL) != 496 KBDS_KBD_BUFFER_FULL) { 497 if (f == KBDS_AUX_BUFFER_FULL) { 498 DELAY(KBDD_DELAYTIME); 499 c = read_data(kbdc); 500 addq(&kbdc->aux, c); 501 } 502 DELAY(KBDC_DELAYTIME); 503 if (CHECKTIMEOUT(&retry)) 504 return 0; 505 } 506 DELAY(KBDD_DELAYTIME); 507 return f; 508 } 509 510 /* 511 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard. 512 * queue anything else. 513 */ 514 static int 515 wait_for_kbd_ack(struct atkbdc_softc *kbdc) 516 { 517 /* CPU will stay inside the loop for 200msec at most */ 518 TOTALDELAY retry = { 200000, 0 }; /* 200ms */ 519 int f; 520 int b; 521 522 while (CHECKTIMEOUT(&retry) == 0) { 523 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) { 524 DELAY(KBDD_DELAYTIME); 525 b = read_data(kbdc); 526 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) { 527 if ((b == KBD_ACK) || (b == KBD_RESEND) || 528 (b == KBD_RESET_FAIL)) 529 return b; 530 addq(&kbdc->kbd, b); 531 } else if ((f & KBDS_BUFFER_FULL) == 532 KBDS_AUX_BUFFER_FULL) { 533 addq(&kbdc->aux, b); 534 } 535 } 536 DELAY(KBDC_DELAYTIME); 537 } 538 return -1; 539 } 540 541 /* wait for data from the aux device */ 542 static int 543 wait_for_aux_data(struct atkbdc_softc *kbdc) 544 { 545 /* CPU will stay inside the loop for 200msec at most */ 546 TOTALDELAY retry = { 200000, 0 }; /* 200ms */ 547 int f; 548 unsigned char b; 549 550 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL) != 551 KBDS_AUX_BUFFER_FULL) { 552 if (f == KBDS_KBD_BUFFER_FULL) { 553 DELAY(KBDD_DELAYTIME); 554 b = read_data(kbdc); 555 addq(&kbdc->kbd, b); 556 } 557 DELAY(KBDC_DELAYTIME); 558 if (CHECKTIMEOUT(&retry)) 559 return 0; 560 } 561 DELAY(KBDD_DELAYTIME); 562 return f; 563 } 564 565 /* 566 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device. 567 * queue anything else. 568 */ 569 static int 570 wait_for_aux_ack(struct atkbdc_softc *kbdc) 571 { 572 /* CPU will stay inside the loop for 200msec at most */ 573 TOTALDELAY retry = { 200000, 0 }; /* 200ms */ 574 int f; 575 int b; 576 577 while (CHECKTIMEOUT(&retry) == 0) { 578 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) { 579 DELAY(KBDD_DELAYTIME); 580 b = read_data(kbdc); 581 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) { 582 if ((b == PSM_ACK) || (b == PSM_RESEND) || 583 (b == PSM_RESET_FAIL)) 584 return b; 585 addq(&kbdc->aux, b); 586 } else if ((f & KBDS_BUFFER_FULL) == 587 KBDS_KBD_BUFFER_FULL) { 588 addq(&kbdc->kbd, b); 589 } 590 } 591 DELAY(KBDC_DELAYTIME); 592 } 593 return -1; 594 } 595 596 /* 597 * Returns read-back data or -1 on failure 598 */ 599 int 600 write_controller_w1r1(KBDC p, int c, int d) 601 { 602 if (!write_controller_command(p, c)) 603 return (-1); 604 if (!write_controller_data(p, d)) 605 return (-1); 606 return (read_controller_data(p)); 607 } 608 609 /* write a one byte command to the controller */ 610 int 611 write_controller_command(KBDC p, int c) 612 { 613 if (!wait_while_controller_busy(p)) 614 return FALSE; 615 write_command(p, c); 616 return TRUE; 617 } 618 619 /* write a one byte data to the controller */ 620 int 621 write_controller_data(KBDC p, int c) 622 { 623 if (!wait_while_controller_busy(p)) 624 return FALSE; 625 write_data(p, c); 626 return TRUE; 627 } 628 629 /* write a one byte keyboard command */ 630 int 631 write_kbd_command(KBDC p, int c) 632 { 633 if (!wait_while_controller_busy(p)) 634 return FALSE; 635 write_data(p, c); 636 return TRUE; 637 } 638 639 /* write a one byte auxiliary device command */ 640 int 641 write_aux_command(KBDC p, int c) 642 { 643 int f; 644 645 f = aux_mux_is_enabled(p) ? KBDC_WRITE_TO_AUX_MUX + p->aux_mux_port : 646 KBDC_WRITE_TO_AUX; 647 648 if (!write_controller_command(p, f)) 649 return FALSE; 650 return write_controller_data(p, c); 651 } 652 653 /* send a command to the keyboard and wait for ACK */ 654 int 655 send_kbd_command(KBDC p, int c) 656 { 657 int retry = KBD_MAXRETRY; 658 int res = -1; 659 660 while (retry-- > 0) { 661 if (!write_kbd_command(p, c)) 662 continue; 663 res = wait_for_kbd_ack(p); 664 if (res == KBD_ACK) 665 break; 666 } 667 return res; 668 } 669 670 /* send a command to the auxiliary device and wait for ACK */ 671 int 672 send_aux_command(KBDC p, int c) 673 { 674 int retry = KBD_MAXRETRY; 675 int res = -1; 676 677 while (retry-- > 0) { 678 if (!write_aux_command(p, c)) 679 continue; 680 /* 681 * FIXME: XXX 682 * The aux device may have already sent one or two bytes of 683 * status data, when a command is received. It will immediately 684 * stop data transmission, thus, leaving an incomplete data 685 * packet in our buffer. We have to discard any unprocessed 686 * data in order to remove such packets. Well, we may remove 687 * unprocessed, but necessary data byte as well... 688 */ 689 emptyq(&p->aux); 690 res = wait_for_aux_ack(p); 691 if (res == PSM_ACK) 692 break; 693 } 694 return res; 695 } 696 697 /* send a command and a data to the keyboard, wait for ACKs */ 698 int 699 send_kbd_command_and_data(KBDC p, int c, int d) 700 { 701 int retry; 702 int res = -1; 703 704 for (retry = KBD_MAXRETRY; retry > 0; --retry) { 705 if (!write_kbd_command(p, c)) 706 continue; 707 res = wait_for_kbd_ack(p); 708 if (res == KBD_ACK) 709 break; 710 else if (res != KBD_RESEND) 711 return res; 712 } 713 if (retry <= 0) 714 return res; 715 716 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) { 717 if (!write_kbd_command(p, d)) 718 continue; 719 res = wait_for_kbd_ack(p); 720 if (res != KBD_RESEND) 721 break; 722 } 723 return res; 724 } 725 726 /* send a command and a data to the auxiliary device, wait for ACKs */ 727 int 728 send_aux_command_and_data(KBDC p, int c, int d) 729 { 730 int retry; 731 int res = -1; 732 733 for (retry = KBD_MAXRETRY; retry > 0; --retry) { 734 if (!write_aux_command(p, c)) 735 continue; 736 emptyq(&p->aux); 737 res = wait_for_aux_ack(p); 738 if (res == PSM_ACK) 739 break; 740 else if (res != PSM_RESEND) 741 return res; 742 } 743 if (retry <= 0) 744 return res; 745 746 for (retry = KBD_MAXRETRY, res = -1; retry > 0; --retry) { 747 if (!write_aux_command(p, d)) 748 continue; 749 res = wait_for_aux_ack(p); 750 if (res != PSM_RESEND) 751 break; 752 } 753 return res; 754 } 755 756 /* 757 * read one byte from any source; whether from the controller, 758 * the keyboard, or the aux device 759 */ 760 int 761 read_controller_data(KBDC p) 762 { 763 if (availq(&p->kbd)) 764 return removeq(&p->kbd); 765 if (availq(&p->aux)) 766 return removeq(&p->aux); 767 if (!wait_for_data(p)) 768 return -1; /* timeout */ 769 return read_data(p); 770 } 771 772 #if KBDIO_DEBUG >= 2 773 static int call = 0; 774 #endif 775 776 /* read one byte from the keyboard */ 777 int 778 read_kbd_data(KBDC p) 779 { 780 unsigned char b; 781 782 #if KBDIO_DEBUG >= 2 783 if (++call > 2000) { 784 call = 0; 785 log(LOG_DEBUG, 786 "kbdc: kbd q: %d calls, max %d chars, " 787 "aux q: %d calls, max %d chars\n", 788 p->kbd.call_count, p->kbd.max_qcount, p->aux.call_count, 789 p->aux.max_qcount); 790 } 791 #endif 792 793 if (availq(&p->kbd)) 794 return removeq(&p->kbd); 795 if (!wait_for_kbd_data(p)) 796 return -1; /* timeout */ 797 b = read_data(p); 798 return b; 799 } 800 801 /* read one byte from the keyboard, but return immediately if 802 * no data is waiting 803 */ 804 int 805 read_kbd_data_no_wait(KBDC p) 806 { 807 int f; 808 unsigned char b; 809 810 #if KBDIO_DEBUG >= 2 811 if (++call > 2000) { 812 call = 0; 813 log(LOG_DEBUG, 814 "kbdc: kbd q: %d calls, max %d chars, " 815 "aux q: %d calls, max %d chars\n", 816 p->kbd.call_count, p->kbd.max_qcount, p->aux.call_count, 817 p->aux.max_qcount); 818 } 819 #endif 820 821 if (availq(&p->kbd)) 822 return removeq(&p->kbd); 823 f = read_status(p) & KBDS_BUFFER_FULL; 824 while (f == KBDS_AUX_BUFFER_FULL) { 825 DELAY(KBDD_DELAYTIME); 826 b = read_data(p); 827 addq(&p->aux, b); 828 f = read_status(p) & KBDS_BUFFER_FULL; 829 } 830 if (f == KBDS_KBD_BUFFER_FULL) { 831 DELAY(KBDD_DELAYTIME); 832 b = read_data(p); 833 return (int)b; 834 } 835 return -1; /* no data */ 836 } 837 838 /* read one byte from the aux device */ 839 int 840 read_aux_data(KBDC p) 841 { 842 unsigned char b; 843 if (availq(&p->aux)) 844 return removeq(&p->aux); 845 if (!wait_for_aux_data(p)) 846 return -1; /* timeout */ 847 b = read_data(p); 848 return b; 849 } 850 851 /* read one byte from the aux device, but return immediately if 852 * no data is waiting 853 */ 854 int 855 read_aux_data_no_wait(KBDC p) 856 { 857 unsigned char b; 858 int f; 859 860 if (availq(&p->aux)) 861 return removeq(&p->aux); 862 f = read_status(p) & KBDS_BUFFER_FULL; 863 while (f == KBDS_KBD_BUFFER_FULL) { 864 DELAY(KBDD_DELAYTIME); 865 b = read_data(p); 866 addq(&p->kbd, b); 867 f = read_status(p) & KBDS_BUFFER_FULL; 868 } 869 if (f == KBDS_AUX_BUFFER_FULL) { 870 DELAY(KBDD_DELAYTIME); 871 b = read_data(p); 872 return b; 873 } 874 return -1; /* no data */ 875 } 876 877 /* discard data from the keyboard */ 878 void 879 empty_kbd_buffer(KBDC p, int wait) 880 { 881 int t; 882 int b; 883 int f; 884 #if KBDIO_DEBUG >= 2 885 int c1 = 0; 886 int c2 = 0; 887 #endif 888 int delta = 2; 889 890 for (t = wait; t > 0;) { 891 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) { 892 DELAY(KBDD_DELAYTIME); 893 b = read_data(p); 894 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) { 895 addq(&p->aux, b); 896 #if KBDIO_DEBUG >= 2 897 ++c2; 898 } else { 899 ++c1; 900 #endif 901 } 902 t = wait; 903 } else { 904 t -= delta; 905 } 906 DELAY(delta * 1000); 907 } 908 #if KBDIO_DEBUG >= 2 909 if ((c1 > 0) || (c2 > 0)) 910 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_kbd_buffer)\n", c1, 911 c2); 912 #endif 913 914 emptyq(&p->kbd); 915 } 916 917 /* discard data from the aux device */ 918 void 919 empty_aux_buffer(KBDC p, int wait) 920 { 921 int t; 922 int b; 923 int f; 924 #if KBDIO_DEBUG >= 2 925 int c1 = 0; 926 int c2 = 0; 927 #endif 928 int delta = 2; 929 930 for (t = wait; t > 0;) { 931 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) { 932 DELAY(KBDD_DELAYTIME); 933 b = read_data(p); 934 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) { 935 addq(&p->kbd, b); 936 #if KBDIO_DEBUG >= 2 937 ++c1; 938 } else { 939 ++c2; 940 #endif 941 } 942 t = wait; 943 } else { 944 t -= delta; 945 } 946 DELAY(delta * 1000); 947 } 948 #if KBDIO_DEBUG >= 2 949 if ((c1 > 0) || (c2 > 0)) 950 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_aux_buffer)\n", c1, 951 c2); 952 #endif 953 954 emptyq(&p->aux); 955 } 956 957 /* discard any data from the keyboard or the aux device */ 958 void 959 empty_both_buffers(KBDC p, int wait) 960 { 961 int t; 962 int f; 963 int waited = 0; 964 #if KBDIO_DEBUG >= 2 965 int c1 = 0; 966 int c2 = 0; 967 #endif 968 int delta = 2; 969 970 for (t = wait; t > 0;) { 971 if ((f = read_status(p)) & KBDS_ANY_BUFFER_FULL) { 972 DELAY(KBDD_DELAYTIME); 973 (void)read_data(p); 974 #if KBDIO_DEBUG >= 2 975 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) 976 ++c1; 977 else 978 ++c2; 979 #endif 980 t = wait; 981 } else { 982 t -= delta; 983 } 984 /* 985 * Some systems (Intel/IBM blades) do not have keyboard devices and 986 * will thus hang in this procedure. Time out after delta seconds to 987 * avoid this hang -- the keyboard attach will fail later on. 988 */ 989 waited += (delta * 1000); 990 if (waited == (delta * 1000000)) 991 return; 992 993 DELAY(delta * 1000); 994 } 995 #if KBDIO_DEBUG >= 2 996 if ((c1 > 0) || (c2 > 0)) 997 log(LOG_DEBUG, "kbdc: %d:%d char read (empty_both_buffers)\n", 998 c1, c2); 999 #endif 1000 1001 emptyq(&p->kbd); 1002 emptyq(&p->aux); 1003 } 1004 1005 /* keyboard and mouse device control */ 1006 1007 /* NOTE: enable the keyboard port but disable the keyboard 1008 * interrupt before calling "reset_kbd()". 1009 */ 1010 int 1011 reset_kbd(KBDC p) 1012 { 1013 int retry = KBD_MAXRETRY; 1014 int again = KBD_MAXWAIT; 1015 int c = KBD_RESEND; /* keep the compiler happy */ 1016 1017 while (retry-- > 0) { 1018 empty_both_buffers(p, 10); 1019 if (!write_kbd_command(p, KBDC_RESET_KBD)) 1020 continue; 1021 emptyq(&p->kbd); 1022 c = read_controller_data(p); 1023 if (verbose || bootverbose) 1024 log(LOG_DEBUG, "kbdc: RESET_KBD return code:%04x\n", c); 1025 if (c == KBD_ACK) /* keyboard has agreed to reset itself... */ 1026 break; 1027 } 1028 if (retry < 0) 1029 return FALSE; 1030 1031 while (again-- > 0) { 1032 /* wait awhile, well, in fact we must wait quite loooooooooooong 1033 */ 1034 DELAY(KBD_RESETDELAY * 1000); 1035 c = read_controller_data(p); /* RESET_DONE/RESET_FAIL */ 1036 if (c != -1) /* wait again if the controller is not ready */ 1037 break; 1038 } 1039 if (verbose || bootverbose) 1040 log(LOG_DEBUG, "kbdc: RESET_KBD status:%04x\n", c); 1041 if (c != KBD_RESET_DONE) 1042 return FALSE; 1043 return TRUE; 1044 } 1045 1046 /* NOTE: enable the aux port but disable the aux interrupt 1047 * before calling `reset_aux_dev()'. 1048 */ 1049 int 1050 reset_aux_dev(KBDC p) 1051 { 1052 int retry = KBD_MAXRETRY; 1053 int again = KBD_MAXWAIT; 1054 int c = PSM_RESEND; /* keep the compiler happy */ 1055 1056 while (retry-- > 0) { 1057 empty_both_buffers(p, 10); 1058 if (!write_aux_command(p, PSMC_RESET_DEV)) 1059 continue; 1060 emptyq(&p->aux); 1061 /* NOTE: Compaq Armada laptops require extra delay here. XXX */ 1062 for (again = KBD_MAXWAIT; again > 0; --again) { 1063 DELAY(KBD_RESETDELAY * 1000); 1064 c = read_aux_data_no_wait(p); 1065 if (c != -1) 1066 break; 1067 } 1068 if (verbose || bootverbose) 1069 log(LOG_DEBUG, "kbdc: RESET_AUX return code:%04x\n", c); 1070 if (c == PSM_ACK) /* aux dev is about to reset... */ 1071 break; 1072 } 1073 if (retry < 0) 1074 return FALSE; 1075 1076 for (again = KBD_MAXWAIT; again > 0; --again) { 1077 /* wait awhile, well, quite looooooooooooong */ 1078 DELAY(KBD_RESETDELAY * 1000); 1079 c = read_aux_data_no_wait(p); /* RESET_DONE/RESET_FAIL */ 1080 if (c != -1) /* wait again if the controller is not ready */ 1081 break; 1082 } 1083 if (verbose || bootverbose) 1084 log(LOG_DEBUG, "kbdc: RESET_AUX status:%04x\n", c); 1085 if (c != PSM_RESET_DONE) /* reset status */ 1086 return FALSE; 1087 1088 c = read_aux_data(p); /* device ID */ 1089 if (verbose || bootverbose) 1090 log(LOG_DEBUG, "kbdc: RESET_AUX ID:%04x\n", c); 1091 /* NOTE: we could check the device ID now, but leave it later... */ 1092 return TRUE; 1093 } 1094 1095 /* controller diagnostics and setup */ 1096 1097 int 1098 test_controller(KBDC p) 1099 { 1100 int retry = KBD_MAXRETRY; 1101 int again = KBD_MAXWAIT; 1102 int c = KBD_DIAG_FAIL; 1103 1104 while (retry-- > 0) { 1105 empty_both_buffers(p, 10); 1106 if (write_controller_command(p, KBDC_DIAGNOSE)) 1107 break; 1108 } 1109 if (retry < 0) 1110 return FALSE; 1111 1112 emptyq(&p->kbd); 1113 while (again-- > 0) { 1114 /* wait awhile */ 1115 DELAY(KBD_RESETDELAY * 1000); 1116 c = read_controller_data(p); /* DIAG_DONE/DIAG_FAIL */ 1117 if (c != -1) /* wait again if the controller is not ready */ 1118 break; 1119 } 1120 if (verbose || bootverbose) 1121 log(LOG_DEBUG, "kbdc: DIAGNOSE status:%04x\n", c); 1122 return (c == KBD_DIAG_DONE); 1123 } 1124 1125 int 1126 test_kbd_port(KBDC p) 1127 { 1128 int retry = KBD_MAXRETRY; 1129 int again = KBD_MAXWAIT; 1130 int c = -1; 1131 1132 while (retry-- > 0) { 1133 empty_both_buffers(p, 10); 1134 if (write_controller_command(p, KBDC_TEST_KBD_PORT)) 1135 break; 1136 } 1137 if (retry < 0) 1138 return FALSE; 1139 1140 emptyq(&p->kbd); 1141 while (again-- > 0) { 1142 c = read_controller_data(p); 1143 if (c != -1) /* try again if the controller is not ready */ 1144 break; 1145 } 1146 if (verbose || bootverbose) 1147 log(LOG_DEBUG, "kbdc: TEST_KBD_PORT status:%04x\n", c); 1148 return c; 1149 } 1150 1151 int 1152 test_aux_port(KBDC p) 1153 { 1154 int retry = KBD_MAXRETRY; 1155 int again = KBD_MAXWAIT; 1156 int c = -1; 1157 1158 while (retry-- > 0) { 1159 empty_both_buffers(p, 10); 1160 if (write_controller_command(p, KBDC_TEST_AUX_PORT)) 1161 break; 1162 } 1163 if (retry < 0) 1164 return FALSE; 1165 1166 emptyq(&p->kbd); 1167 while (again-- > 0) { 1168 c = read_controller_data(p); 1169 if (c != -1) /* try again if the controller is not ready */ 1170 break; 1171 } 1172 if (verbose || bootverbose) 1173 log(LOG_DEBUG, "kbdc: TEST_AUX_PORT status:%04x\n", c); 1174 return c; 1175 } 1176 1177 int 1178 kbdc_get_device_mask(KBDC p) 1179 { 1180 return p->command_mask; 1181 } 1182 1183 void 1184 kbdc_set_device_mask(KBDC p, int mask) 1185 { 1186 p->command_mask = mask & 1187 (((p->quirks & KBDC_QUIRK_KEEP_ACTIVATED) ? 0 : 1188 KBD_KBD_CONTROL_BITS) | 1189 KBD_AUX_CONTROL_BITS); 1190 } 1191 1192 int 1193 get_controller_command_byte(KBDC p) 1194 { 1195 if (p->command_byte != -1) 1196 return p->command_byte; 1197 if (!write_controller_command(p, KBDC_GET_COMMAND_BYTE)) 1198 return -1; 1199 emptyq(&p->kbd); 1200 p->command_byte = read_controller_data(p); 1201 return p->command_byte; 1202 } 1203 1204 int 1205 set_controller_command_byte(KBDC p, int mask, int command) 1206 { 1207 if (get_controller_command_byte(p) == -1) 1208 return FALSE; 1209 1210 command = (p->command_byte & ~mask) | (command & mask); 1211 if (command & KBD_DISABLE_KBD_PORT) { 1212 if (!write_controller_command(p, KBDC_DISABLE_KBD_PORT)) 1213 return FALSE; 1214 } 1215 if (!write_controller_command(p, KBDC_SET_COMMAND_BYTE)) 1216 return FALSE; 1217 if (!write_controller_data(p, command)) 1218 return FALSE; 1219 p->command_byte = command; 1220 1221 if (verbose) 1222 log(LOG_DEBUG, 1223 "kbdc: new command byte:%04x (set_controller...)\n", 1224 command); 1225 1226 return TRUE; 1227 } 1228 1229 /* 1230 * Rudimentary support for active PS/2 AUX port multiplexing. 1231 * Only write commands can be routed to a selected AUX port. 1232 * Source port of data processed by read commands is totally ignored. 1233 */ 1234 static int 1235 set_aux_mux_state(KBDC p, int enabled) 1236 { 1237 int command, version; 1238 1239 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 || 1240 write_controller_data(p, 0xF0) == 0 || 1241 read_controller_data(p) != 0xF0) 1242 return (-1); 1243 1244 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 || 1245 write_controller_data(p, 0x56) == 0 || 1246 read_controller_data(p) != 0x56) 1247 return (-1); 1248 1249 command = enabled ? 0xa4 : 0xa5; 1250 if (write_controller_command(p, KBDC_FORCE_AUX_OUTPUT) == 0 || 1251 write_controller_data(p, command) == 0 || 1252 (version = read_controller_data(p)) == command) 1253 return (-1); 1254 1255 return (version); 1256 } 1257 1258 int 1259 set_active_aux_mux_port(KBDC p, int port) 1260 { 1261 1262 if (!aux_mux_is_enabled(p)) 1263 return (FALSE); 1264 1265 if (port < 0 || port >= KBDC_AUX_MUX_NUM_PORTS) 1266 return (FALSE); 1267 1268 p->aux_mux_port = port; 1269 1270 return (TRUE); 1271 } 1272 1273 /* Checks for active multiplexing support and enables it */ 1274 int 1275 enable_aux_mux(KBDC p) 1276 { 1277 int version; 1278 1279 version = set_aux_mux_state(p, TRUE); 1280 if (version >= 0) { 1281 p->aux_mux_enabled = TRUE; 1282 set_active_aux_mux_port(p, 0); 1283 } 1284 1285 return (version); 1286 } 1287 1288 int 1289 disable_aux_mux(KBDC p) 1290 { 1291 1292 p->aux_mux_enabled = FALSE; 1293 1294 return (set_aux_mux_state(p, FALSE)); 1295 } 1296 1297 int 1298 aux_mux_is_enabled(KBDC p) 1299 { 1300 1301 return (p->aux_mux_enabled); 1302 } 1303