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