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