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