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