1 /* $OpenBSD: gsckbc.c,v 1.20 2019/10/12 15:53:24 cheloha Exp $ */ 2 /* 3 * Copyright (c) 2003, Miodrag Vallat. 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF MIND, 21 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /* 28 * Derived from /sys/dev/ic/pckbd.c under the following terms: 29 * OpenBSD: pckbc.c,v 1.5 2002/06/09 00:58:03 nordin Exp 30 * NetBSD: pckbc.c,v 1.5 2000/06/09 04:58:35 soda Exp 31 */ 32 /* 33 * Copyright (c) 1998 34 * Matthias Drochner. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed for the NetBSD Project 47 * by Matthias Drochner. 48 * 4. The name of the author may not be used to endorse or promote products 49 * derived from this software without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 /* 64 * Driver for the PS/2-like keyboard and mouse ports found on 712 and 715 65 * models, among others. 66 * 67 * Contrary to the ``pckbc'' port set found on other arches, the 68 * keyboard and mouse port are two separate entities on the snakes, and 69 * they are driven by a custom chip not 8042-compatible. 70 */ 71 72 #include "pckbd.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/device.h> 77 #include <sys/kernel.h> 78 #include <sys/malloc.h> 79 #include <sys/proc.h> 80 81 #include <machine/autoconf.h> 82 #include <machine/bus.h> 83 #include <machine/intr.h> 84 #include <machine/iomod.h> 85 86 #include <hppa/dev/cpudevs.h> 87 #include <hppa/gsc/gscbusvar.h> 88 89 #include <hppa/gsc/gsckbcreg.h> 90 #include <dev/ic/pckbcvar.h> 91 92 #include <dev/pckbc/pckbdreg.h> /* constants for probe magic */ 93 #include <dev/pckbc/pckbdvar.h> 94 95 int gsckbc_match(struct device *, void *, void *); 96 void gsckbc_attach(struct device *, struct device *, void *); 97 98 struct gsckbc_softc { 99 struct pckbc_softc sc_pckbc; 100 101 void *sc_ih; 102 int sc_type; 103 }; 104 105 struct cfattach gsckbc_ca = { 106 sizeof(struct gsckbc_softc), gsckbc_match, gsckbc_attach 107 }; 108 109 struct cfdriver gsckbc_cd = { 110 NULL, "gsckbc", DV_DULL 111 }; 112 113 /* descriptor for one device command */ 114 struct pckbc_devcmd { 115 TAILQ_ENTRY(pckbc_devcmd) next; 116 int flags; 117 #define KBC_CMDFLAG_SYNC 1 /* give descriptor back to caller */ 118 #define KBC_CMDFLAG_SLOW 2 119 u_char cmd[4]; 120 int cmdlen, cmdidx, retries; 121 u_char response[4]; 122 int status, responselen, responseidx; 123 }; 124 125 /* data per slave device */ 126 struct pckbc_slotdata { 127 int polling; /* don't read data port in interrupt handler */ 128 TAILQ_HEAD(, pckbc_devcmd) cmdqueue; /* active commands */ 129 TAILQ_HEAD(, pckbc_devcmd) freequeue; /* free commands */ 130 #define NCMD 5 131 struct pckbc_devcmd cmds[NCMD]; 132 }; 133 134 #define CMD_IN_QUEUE(q) (TAILQ_FIRST(&(q)->cmdqueue) != NULL) 135 /* Force polling mode behaviour for boot -a XXX */ 136 #define IS_POLLING(q) ((q)->polling || cold) 137 138 void pckbc_init_slotdata(struct pckbc_slotdata *); 139 int pckbc_attach_slot(struct pckbc_softc *, pckbc_slot_t); 140 int pckbc_submatch(struct device *, void *, void *); 141 int pckbcprint(void *, const char *); 142 143 int pckbc_wait_output(bus_space_tag_t, bus_space_handle_t); 144 int pckbc_send_devcmd(struct pckbc_internal *, pckbc_slot_t, 145 u_char); 146 void pckbc_poll_cmd1(struct pckbc_internal *, pckbc_slot_t, 147 struct pckbc_devcmd *); 148 149 void pckbc_cleanqueue(struct pckbc_slotdata *); 150 void pckbc_cleanup(void *); 151 int pckbc_cmdresponse(struct pckbc_internal *, pckbc_slot_t, u_char); 152 void pckbc_start(struct pckbc_internal *, pckbc_slot_t); 153 int gsckbcintr(void *); 154 155 const char *pckbc_slot_names[] = { "kbd", "mouse" }; 156 157 #define KBC_DEVCMD_ACK 0xfa 158 #define KBC_DEVCMD_RESEND 0xfe 159 160 #define KBD_DELAY DELAY(8) 161 162 int 163 gsckbc_match(struct device *parent, void *match, void *aux) 164 { 165 struct gsc_attach_args *ga = aux; 166 bus_space_handle_t ioh; 167 u_int8_t rv; 168 169 if (ga->ga_type.iodc_type != HPPA_TYPE_FIO || 170 ga->ga_type.iodc_sv_model != HPPA_FIO_GPCIO) 171 return (0); 172 173 /* Map the i/o space. */ 174 if (bus_space_map(ga->ga_ca.ca_iot, ga->ga_ca.ca_hpa, 175 KBMAPSIZE, 0, &ioh)) 176 return 0; 177 178 rv = bus_space_read_1(ga->ga_ca.ca_iot, ioh, KBIDP); 179 bus_space_unmap(ga->ga_ca.ca_iot, ioh, KBMAPSIZE); 180 181 if (rv == PCKBC_KBD_SLOT || rv == PCKBC_AUX_SLOT) 182 return (1); /* keyboard or mouse port */ 183 184 return (0); 185 } 186 187 /* 188 * Attachment helper functions 189 */ 190 191 /* state machine values */ 192 #define PROBE_SUCCESS 0 193 #define PROBE_TIMEOUT 1 194 #define PROBE_RETRANS 2 195 #define PROBE_NOACK 3 196 197 int probe_readtmo(bus_space_tag_t iot, bus_space_handle_t ioh, int *reply); 198 int probe_readretry(bus_space_tag_t iot, bus_space_handle_t ioh, int *reply); 199 int probe_sendtmo(bus_space_tag_t iot, bus_space_handle_t ioh, int cmdbyte); 200 int probe_sendack(bus_space_tag_t iot, bus_space_handle_t ioh, int cmdbyte); 201 int probe_ident(bus_space_tag_t iot, bus_space_handle_t ioh); 202 203 #define PROBE_TRIES 1000 204 205 int 206 probe_readtmo(bus_space_tag_t iot, bus_space_handle_t ioh, int *reply) 207 { 208 int numtries = PROBE_TRIES; 209 210 while (numtries--) { 211 if (bus_space_read_1(iot, ioh, KBSTATP) & 212 (KBS_DIB | KBS_TERR | KBS_PERR)) 213 break; 214 DELAY(500); 215 } 216 217 if (numtries <= 0) 218 return (PROBE_TIMEOUT); 219 220 if (bus_space_read_1(iot, ioh, KBSTATP) & (KBS_PERR | KBS_TERR)) { 221 if (!(bus_space_read_1(iot, ioh, KBSTATP) & KBS_DIB)) { 222 bus_space_write_1(iot, ioh, KBRESETP, 0xff); 223 bus_space_write_1(iot, ioh, KBRESETP, 0x00); 224 bus_space_write_1(iot, ioh, KBCMDP, 225 bus_space_read_1(iot, ioh, KBCMDP) | KBCP_ENABLE); 226 return (PROBE_TIMEOUT); 227 } 228 229 *reply = bus_space_read_1(iot, ioh, KBDATAP); 230 if (!(bus_space_read_1(iot, ioh, KBSTATP) & KBS_DIB)) { 231 bus_space_write_1(iot, ioh, KBRESETP, 0xff); 232 bus_space_write_1(iot, ioh, KBRESETP, 0x00); 233 bus_space_write_1(iot, ioh, KBCMDP, 234 bus_space_read_1(iot, ioh, KBCMDP) | KBCP_ENABLE); 235 if (probe_sendtmo(iot, ioh, KBR_RESEND)) 236 return (PROBE_TIMEOUT); 237 else 238 return (PROBE_RETRANS); 239 } else 240 return (PROBE_SUCCESS); 241 } else { 242 *reply = bus_space_read_1(iot, ioh, KBDATAP); 243 return (PROBE_SUCCESS); 244 } 245 } 246 247 int 248 probe_readretry(bus_space_tag_t iot, bus_space_handle_t ioh, int *reply) 249 { 250 int read_status; 251 int retrans = KB_MAX_RETRANS; 252 253 do { 254 read_status = probe_readtmo(iot, ioh, reply); 255 } while ((read_status == PROBE_RETRANS) && retrans--); 256 257 return (read_status); 258 } 259 260 int 261 probe_sendtmo(bus_space_tag_t iot, bus_space_handle_t ioh, int cmdbyte) 262 { 263 int numtries = PROBE_TRIES; 264 265 while (numtries--) { 266 if ((bus_space_read_1(iot, ioh, KBSTATP) & KBS_OCMD) == 0) 267 break; 268 DELAY(500); 269 } 270 271 if (numtries <= 0) 272 return (1); 273 274 bus_space_write_1(iot, ioh, KBDATAP, cmdbyte); 275 bus_space_write_1(iot, ioh, KBCMDP, KBCP_ENABLE); 276 return (0); 277 } 278 279 int 280 probe_sendack(bus_space_tag_t iot, bus_space_handle_t ioh, int cmdbyte) 281 { 282 int retranscount; 283 int reply; 284 285 for (retranscount = 0; retranscount < KB_MAX_RETRANS; retranscount++) { 286 if (probe_sendtmo(iot, ioh, cmdbyte)) 287 return (PROBE_TIMEOUT); 288 if (probe_readretry(iot, ioh, &reply)) 289 return (PROBE_TIMEOUT); 290 291 switch (reply) { 292 case KBR_ACK: 293 return (PROBE_SUCCESS); 294 case KBR_RESEND: 295 break; 296 default: 297 return (PROBE_NOACK); 298 } 299 } 300 return (PROBE_TIMEOUT); 301 302 } 303 304 int 305 probe_ident(bus_space_tag_t iot, bus_space_handle_t ioh) 306 { 307 int status; 308 309 bus_space_write_1(iot, ioh, KBRESETP, 0); 310 bus_space_write_1(iot, ioh, KBCMDP, KBCP_ENABLE); 311 DELAY(0x20000); /* XXX why 0x? */ 312 bus_space_write_1(iot, ioh, KBCMDP, 0); 313 DELAY(20000); 314 bus_space_write_1(iot, ioh, KBRESETP, 0); 315 bus_space_write_1(iot, ioh, KBCMDP, KBCP_DIAG); 316 DELAY(20000); 317 318 status = probe_sendack(iot, ioh, KBC_DISABLE); 319 switch (status) { 320 case PROBE_TIMEOUT: 321 if (bus_space_read_1(iot, ioh, KBSTATP) & KBS_OCMD) { 322 bus_space_write_1(iot, ioh, KBRESETP, 0); 323 bus_space_write_1(iot, ioh, KBCMDP, KBCP_ENABLE); 324 } 325 return (-1); 326 case PROBE_NOACK: 327 return (-1); 328 } 329 330 if (probe_sendack(iot, ioh, KBC_ID) != PROBE_SUCCESS) 331 return (-1); 332 333 if (probe_readretry(iot, ioh, &status)) 334 return (-1); 335 336 switch (status) { 337 case KBR_MOUSE_ID: 338 return PCKBC_AUX_SLOT; 339 case KBR_KBD_ID1: 340 if (probe_readretry(iot, ioh, &status)) 341 return (-1); 342 if (status == KBR_KBD_ID2) { 343 if (probe_sendack(iot, ioh, KBC_ENABLE) == 344 PROBE_TIMEOUT) { 345 bus_space_write_1(iot, ioh, KBRESETP, 0); 346 bus_space_write_1(iot, ioh, KBCMDP, 347 KBCP_ENABLE); 348 } 349 return PCKBC_KBD_SLOT; 350 } 351 } 352 return (-1); 353 } 354 355 void 356 gsckbc_attach(struct device *parent, struct device *self, void *aux) 357 { 358 struct gsc_attach_args *ga = aux; 359 struct gsckbc_softc *gsc = (void *)self; 360 struct pckbc_softc *sc = &gsc->sc_pckbc; 361 struct pckbc_internal *t; 362 bus_space_tag_t iot; 363 bus_space_handle_t ioh; 364 int ident; 365 366 iot = ga->ga_ca.ca_iot; 367 368 if (bus_space_map(iot, ga->ga_ca.ca_hpa, KBMAPSIZE, 0, &ioh)) 369 panic("gsckbc_attach: couldn't map port"); 370 371 gsc->sc_type = bus_space_read_1(iot, ioh, KBIDP); 372 373 switch (gsc->sc_type) { 374 case PCKBC_KBD_SLOT: 375 case PCKBC_AUX_SLOT: 376 break; 377 default: 378 printf(": unknown port type %x\n", gsc->sc_type); 379 /* play nice and don't really attach. */ 380 bus_space_unmap(iot, ioh, KBMAPSIZE); 381 return; 382 } 383 384 gsc->sc_ih = gsc_intr_establish((struct gsc_softc *)parent, 385 ga->ga_ca.ca_irq, IPL_TTY, gsckbcintr, sc, sc->sc_dv.dv_xname); 386 if (gsc->sc_ih == NULL) { 387 printf(": can't establish interrupt\n"); 388 bus_space_unmap(iot, ioh, KBMAPSIZE); 389 return; 390 } 391 392 printf("\n"); 393 394 t = malloc(sizeof(*t), M_DEVBUF, M_WAITOK | M_ZERO); 395 t->t_iot = iot; 396 /* XXX it does not make sense to only map two ports here */ 397 t->t_ioh_d = t->t_ioh_c = ioh; 398 t->t_addr = ga->ga_ca.ca_hpa; 399 t->t_sc = sc; 400 timeout_set(&t->t_cleanup, pckbc_cleanup, t); 401 sc->id = t; 402 403 /* 404 * Reset port and probe device, if plugged 405 */ 406 ident = probe_ident(iot, ioh); 407 if (ident != gsc->sc_type) { 408 /* don't whine for unplugged ports */ 409 if (ident != -1) 410 printf("%s: expecting device type %d, got %d\n", 411 sc->sc_dv.dv_xname, gsc->sc_type, ident); 412 } else { 413 #if (NPCKBD > 0) 414 if (gsc->sc_type == PCKBC_KBD_SLOT && 415 ga->ga_dp.dp_mod == PAGE0->mem_kbd.pz_dp.dp_mod && 416 bcmp(ga->ga_dp.dp_bc, PAGE0->mem_kbd.pz_dp.dp_bc, 6) == 0) 417 pckbd_cnattach(t); 418 #endif 419 pckbc_attach_slot(sc, gsc->sc_type); 420 } 421 } 422 423 /* 424 * pckbc-like interfaces 425 */ 426 427 int 428 pckbc_wait_output(iot, ioh) 429 bus_space_tag_t iot; 430 bus_space_handle_t ioh; 431 { 432 u_int i; 433 434 for (i = 100000; i; i--) { 435 if ((bus_space_read_1(iot, ioh, KBSTATP) & KBS_OCMD)) { 436 KBD_DELAY; 437 } else 438 return (1); 439 } 440 return (0); 441 } 442 443 int 444 pckbc_send_cmd(iot, ioh, val) 445 bus_space_tag_t iot; 446 bus_space_handle_t ioh; 447 u_char val; 448 { 449 if (!pckbc_wait_output(iot, ioh)) 450 return (0); 451 bus_space_write_1(iot, ioh, KBOUTP, val); 452 bus_space_write_1(iot, ioh, KBCMDP, KBCP_ENABLE); 453 return (1); 454 } 455 456 /* XXX logic */ 457 int 458 pckbc_poll_data1(iot, ioh, ioh_c, slot, checkaux) 459 bus_space_tag_t iot; 460 bus_space_handle_t ioh, ioh_c; 461 pckbc_slot_t slot; 462 int checkaux; /* ignored on hppa */ 463 { 464 int i; 465 u_char stat; 466 467 /* if 1 port read takes 1us (?), this polls for 100ms */ 468 for (i = 100000; i; i--) { 469 stat = bus_space_read_1(iot, ioh, KBSTATP); 470 if (stat & KBS_DIB) { 471 KBD_DELAY; 472 return bus_space_read_1(iot, ioh, KBDATAP); 473 } 474 } 475 return (-1); 476 } 477 478 int 479 pckbc_send_devcmd(t, slot, val) 480 struct pckbc_internal *t; 481 pckbc_slot_t slot; 482 u_char val; 483 { 484 return pckbc_send_cmd(t->t_iot, t->t_ioh_d, val); 485 } 486 487 int 488 pckbc_submatch(parent, match, aux) 489 struct device *parent; 490 void *match; 491 void *aux; 492 { 493 struct cfdata *cf = match; 494 struct pckbc_attach_args *pa = aux; 495 496 if (cf->cf_loc[PCKBCCF_SLOT] != PCKBCCF_SLOT_DEFAULT && 497 cf->cf_loc[PCKBCCF_SLOT] != pa->pa_slot) 498 return (0); 499 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 500 } 501 502 int 503 pckbc_attach_slot(sc, slot) 504 struct pckbc_softc *sc; 505 pckbc_slot_t slot; 506 { 507 struct pckbc_internal *t = sc->id; 508 struct pckbc_attach_args pa; 509 int found; 510 511 pa.pa_tag = t; 512 pa.pa_slot = slot; 513 found = (config_found_sm((struct device *)sc, &pa, 514 pckbcprint, pckbc_submatch) != NULL); 515 516 if (found && !t->t_slotdata[slot]) { 517 t->t_slotdata[slot] = malloc(sizeof(struct pckbc_slotdata), 518 M_DEVBUF, M_NOWAIT); 519 if (t->t_slotdata[slot] == NULL) 520 return 0; 521 pckbc_init_slotdata(t->t_slotdata[slot]); 522 } 523 return (found); 524 } 525 526 int 527 pckbcprint(aux, pnp) 528 void *aux; 529 const char *pnp; 530 { 531 #if 0 /* hppa having devices for each slot, this is barely useful */ 532 struct pckbc_attach_args *pa = aux; 533 534 if (!pnp) 535 printf(" (%s slot)", pckbc_slot_names[pa->pa_slot]); 536 #endif 537 return (QUIET); 538 } 539 540 void 541 pckbc_init_slotdata(q) 542 struct pckbc_slotdata *q; 543 { 544 int i; 545 TAILQ_INIT(&q->cmdqueue); 546 TAILQ_INIT(&q->freequeue); 547 548 for (i = 0; i < NCMD; i++) { 549 TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next); 550 } 551 q->polling = 0; 552 } 553 554 void 555 pckbc_flush(self, slot) 556 pckbc_tag_t self; 557 pckbc_slot_t slot; 558 { 559 struct pckbc_internal *t = self; 560 561 pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_d, slot, 0); 562 } 563 564 int 565 pckbc_poll_data(self, slot) 566 pckbc_tag_t self; 567 pckbc_slot_t slot; 568 { 569 struct pckbc_internal *t = self; 570 struct pckbc_slotdata *q = t->t_slotdata[slot]; 571 int c; 572 573 c = pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_d, slot, 0); 574 if (c != -1 && q && CMD_IN_QUEUE(q)) { 575 /* we jumped into a running command - try to 576 deliver the response */ 577 if (pckbc_cmdresponse(t, slot, c)) 578 return (-1); 579 } 580 return (c); 581 } 582 583 int 584 pckbc_xt_translation(self) 585 pckbc_tag_t self; 586 { 587 /* Translation isn't supported... */ 588 return (-1); 589 } 590 591 void 592 pckbc_slot_enable(self, slot, on) 593 pckbc_tag_t self; 594 pckbc_slot_t slot; 595 int on; 596 { 597 /* can't enable slots here as they are different devices */ 598 } 599 600 void 601 pckbc_set_poll(self, slot, on) 602 pckbc_tag_t self; 603 pckbc_slot_t slot; 604 int on; 605 { 606 struct pckbc_internal *t = (struct pckbc_internal *)self; 607 608 t->t_slotdata[slot]->polling = on; 609 610 if (!on) { 611 int s; 612 613 /* 614 * If disabling polling on a device that's been configured, 615 * make sure there are no bytes left in the FIFO, holding up 616 * the interrupt line. Otherwise we won't get any further 617 * interrupts. 618 */ 619 if (t->t_sc) { 620 s = spltty(); 621 gsckbcintr(t->t_sc); 622 splx(s); 623 } 624 } 625 } 626 627 /* 628 * Pass command to device, poll for ACK and data. 629 * to be called at spltty() 630 */ 631 void 632 pckbc_poll_cmd1(t, slot, cmd) 633 struct pckbc_internal *t; 634 pckbc_slot_t slot; 635 struct pckbc_devcmd *cmd; 636 { 637 bus_space_tag_t iot = t->t_iot; 638 bus_space_handle_t ioh = t->t_ioh_d; 639 int i, c = 0; 640 641 while (cmd->cmdidx < cmd->cmdlen) { 642 if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) { 643 printf("pckbc_cmd: send error\n"); 644 cmd->status = EIO; 645 return; 646 } 647 for (i = 10; i; i--) { /* 1s ??? */ 648 c = pckbc_poll_data1(iot, ioh, ioh, slot, 0); 649 if (c != -1) 650 break; 651 } 652 653 if (c == KBC_DEVCMD_ACK) { 654 cmd->cmdidx++; 655 continue; 656 } 657 if (c == KBC_DEVCMD_RESEND) { 658 #ifdef PCKBCDEBUG 659 printf("pckbc_cmd: RESEND\n"); 660 #endif 661 if (cmd->retries++ < KB_MAX_RETRANS) 662 continue; 663 else { 664 #ifdef PCKBCDEBUG 665 printf("pckbc: cmd failed\n"); 666 #endif 667 cmd->status = EIO; 668 return; 669 } 670 } 671 if (c == -1) { 672 #ifdef PCKBCDEBUG 673 printf("pckbc_cmd: timeout\n"); 674 #endif 675 cmd->status = EIO; 676 return; 677 } 678 #ifdef PCKBCDEBUG 679 printf("pckbc_cmd: lost 0x%x\n", c); 680 #endif 681 } 682 683 while (cmd->responseidx < cmd->responselen) { 684 if (cmd->flags & KBC_CMDFLAG_SLOW) 685 i = 100; /* 10s ??? */ 686 else 687 i = 10; /* 1s ??? */ 688 while (i--) { 689 c = pckbc_poll_data1(iot, ioh, ioh, slot, 0); 690 if (c != -1) 691 break; 692 } 693 if (c == -1) { 694 #ifdef PCKBCDEBUG 695 printf("pckbc_cmd: no data\n"); 696 #endif 697 cmd->status = ETIMEDOUT; 698 return; 699 } else 700 cmd->response[cmd->responseidx++] = c; 701 } 702 } 703 704 /* for use in autoconfiguration */ 705 int 706 pckbc_poll_cmd(self, slot, cmd, len, responselen, respbuf, slow) 707 pckbc_tag_t self; 708 pckbc_slot_t slot; 709 u_char *cmd; 710 int len, responselen; 711 u_char *respbuf; 712 int slow; 713 { 714 struct pckbc_devcmd nc; 715 716 if ((len > 4) || (responselen > 4)) 717 return (EINVAL); 718 719 bzero(&nc, sizeof(nc)); 720 bcopy(cmd, nc.cmd, len); 721 nc.cmdlen = len; 722 nc.responselen = responselen; 723 nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0); 724 725 pckbc_poll_cmd1(self, slot, &nc); 726 727 if (nc.status == 0 && respbuf) 728 bcopy(nc.response, respbuf, responselen); 729 730 return (nc.status); 731 } 732 733 /* 734 * Clean up a command queue, throw away everything. 735 */ 736 void 737 pckbc_cleanqueue(q) 738 struct pckbc_slotdata *q; 739 { 740 struct pckbc_devcmd *cmd; 741 #ifdef PCKBCDEBUG 742 int i; 743 #endif 744 745 while ((cmd = TAILQ_FIRST(&q->cmdqueue))) { 746 TAILQ_REMOVE(&q->cmdqueue, cmd, next); 747 #ifdef PCKBCDEBUG 748 printf("pckbc_cleanqueue: removing"); 749 for (i = 0; i < cmd->cmdlen; i++) 750 printf(" %02x", cmd->cmd[i]); 751 printf("\n"); 752 #endif 753 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next); 754 } 755 } 756 757 /* 758 * Timeout error handler: clean queues and data port. 759 * XXX could be less invasive. 760 */ 761 void 762 pckbc_cleanup(self) 763 void *self; 764 { 765 struct pckbc_internal *t = self; 766 int s; 767 768 printf("pckbc: command timeout\n"); 769 770 s = spltty(); 771 772 if (t->t_slotdata[PCKBC_KBD_SLOT]) 773 pckbc_cleanqueue(t->t_slotdata[PCKBC_KBD_SLOT]); 774 if (t->t_slotdata[PCKBC_AUX_SLOT]) 775 pckbc_cleanqueue(t->t_slotdata[PCKBC_AUX_SLOT]); 776 777 while (bus_space_read_1(t->t_iot, t->t_ioh_d, KBSTATP) & KBS_DIB) { 778 KBD_DELAY; 779 (void) bus_space_read_1(t->t_iot, t->t_ioh_d, KBDATAP); 780 } 781 782 /* reset KBC? */ 783 784 splx(s); 785 } 786 787 /* 788 * Pass command to device during normal operation. 789 * to be called at spltty() 790 */ 791 void 792 pckbc_start(t, slot) 793 struct pckbc_internal *t; 794 pckbc_slot_t slot; 795 { 796 struct pckbc_slotdata *q = t->t_slotdata[slot]; 797 struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue); 798 799 if (IS_POLLING(q)) { 800 do { 801 pckbc_poll_cmd1(t, slot, cmd); 802 if (cmd->status) 803 printf("pckbc_start: command error\n"); 804 805 TAILQ_REMOVE(&q->cmdqueue, cmd, next); 806 if (cmd->flags & KBC_CMDFLAG_SYNC) 807 wakeup(cmd); 808 else { 809 timeout_del(&t->t_cleanup); 810 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next); 811 } 812 cmd = TAILQ_FIRST(&q->cmdqueue); 813 } while (cmd); 814 return; 815 } 816 817 if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) { 818 printf("pckbc_start: send error\n"); 819 /* XXX what now? */ 820 return; 821 } 822 } 823 824 /* 825 * Handle command responses coming in asynchronously, 826 * return nonzero if valid response. 827 * to be called at spltty() 828 */ 829 int 830 pckbc_cmdresponse(t, slot, data) 831 struct pckbc_internal *t; 832 pckbc_slot_t slot; 833 u_char data; 834 { 835 struct pckbc_slotdata *q = t->t_slotdata[slot]; 836 struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue); 837 838 #ifdef DIAGNOSTIC 839 if (!cmd) 840 panic("pckbc_cmdresponse: no active command"); 841 #endif 842 if (cmd->cmdidx < cmd->cmdlen) { 843 if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND) 844 return (0); 845 846 if (data == KBC_DEVCMD_RESEND) { 847 if (cmd->retries++ < KB_MAX_RETRANS) { 848 /* try again last command */ 849 goto restart; 850 } else { 851 #ifdef PCKBCDEBUG 852 printf("pckbc: cmd failed\n"); 853 #endif 854 cmd->status = EIO; 855 /* dequeue */ 856 } 857 } else { 858 if (++cmd->cmdidx < cmd->cmdlen) 859 goto restart; 860 if (cmd->responselen) 861 return (1); 862 /* else dequeue */ 863 } 864 } else if (cmd->responseidx < cmd->responselen) { 865 cmd->response[cmd->responseidx++] = data; 866 if (cmd->responseidx < cmd->responselen) 867 return (1); 868 /* else dequeue */ 869 } else 870 return (0); 871 872 /* dequeue: */ 873 TAILQ_REMOVE(&q->cmdqueue, cmd, next); 874 if (cmd->flags & KBC_CMDFLAG_SYNC) 875 wakeup(cmd); 876 else { 877 timeout_del(&t->t_cleanup); 878 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next); 879 } 880 if (!CMD_IN_QUEUE(q)) 881 return (1); 882 restart: 883 pckbc_start(t, slot); 884 return (1); 885 } 886 887 /* 888 * Put command into the device's command queue, return zero or errno. 889 */ 890 int 891 pckbc_enqueue_cmd(self, slot, cmd, len, responselen, sync, respbuf) 892 pckbc_tag_t self; 893 pckbc_slot_t slot; 894 u_char *cmd; 895 int len, responselen, sync; 896 u_char *respbuf; 897 { 898 struct pckbc_internal *t = self; 899 struct pckbc_slotdata *q = t->t_slotdata[slot]; 900 struct pckbc_devcmd *nc; 901 int s, isactive, res = 0; 902 903 if ((len > 4) || (responselen > 4)) 904 return (EINVAL); 905 s = spltty(); 906 nc = TAILQ_FIRST(&q->freequeue); 907 if (nc) { 908 TAILQ_REMOVE(&q->freequeue, nc, next); 909 } 910 splx(s); 911 if (!nc) 912 return (ENOMEM); 913 914 bzero(nc, sizeof(*nc)); 915 bcopy(cmd, nc->cmd, len); 916 nc->cmdlen = len; 917 nc->responselen = responselen; 918 nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0); 919 920 s = spltty(); 921 922 if (IS_POLLING(q) && sync) { 923 /* 924 * XXX We should poll until the queue is empty. 925 * But we don't come here normally, so make 926 * it simple and throw away everything. 927 */ 928 pckbc_cleanqueue(q); 929 } 930 931 isactive = CMD_IN_QUEUE(q); 932 TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next); 933 if (!isactive) 934 pckbc_start(t, slot); 935 936 if (IS_POLLING(q)) 937 res = (sync ? nc->status : 0); 938 else if (sync) { 939 if ((res = tsleep_nsec(nc, 0, "kbccmd", SEC_TO_NSEC(1)))) { 940 TAILQ_REMOVE(&q->cmdqueue, nc, next); 941 pckbc_cleanup(t); 942 } else 943 res = nc->status; 944 } else 945 timeout_add_sec(&t->t_cleanup, 1); 946 947 if (sync) { 948 if (respbuf) 949 bcopy(nc->response, respbuf, responselen); 950 TAILQ_INSERT_TAIL(&q->freequeue, nc, next); 951 } 952 953 splx(s); 954 955 return (res); 956 } 957 958 void 959 pckbc_set_inputhandler(self, slot, func, arg, name) 960 pckbc_tag_t self; 961 pckbc_slot_t slot; 962 pckbc_inputfcn func; 963 void *arg; 964 char *name; 965 { 966 struct pckbc_internal *t = (struct pckbc_internal *)self; 967 struct pckbc_softc *sc = t->t_sc; 968 969 if (slot >= PCKBC_NSLOTS) 970 panic("pckbc_set_inputhandler: bad slot %d", slot); 971 972 sc->inputhandler[slot] = func; 973 sc->inputarg[slot] = arg; 974 sc->subname[slot] = name; 975 } 976 977 int 978 gsckbcintr(void *v) 979 { 980 struct gsckbc_softc *gsc = v; 981 struct pckbc_softc *sc = (struct pckbc_softc *)gsc; 982 struct pckbc_internal *t = sc->id; 983 pckbc_slot_t slot; 984 struct pckbc_slotdata *q; 985 int served = 0, data; 986 987 while (bus_space_read_1(t->t_iot, t->t_ioh_d, KBSTATP) & KBS_DIB) { 988 served = 1; 989 990 slot = gsc->sc_type; 991 q = t->t_slotdata[slot]; 992 993 if (!q) { 994 /* XXX do something for live insertion? */ 995 #ifdef PCKBCDEBUG 996 printf("gsckbcintr: no dev for slot %d\n", slot); 997 #endif 998 KBD_DELAY; 999 (void) bus_space_read_1(t->t_iot, t->t_ioh_d, KBDATAP); 1000 continue; 1001 } 1002 1003 if (IS_POLLING(q)) 1004 break; /* pckbc_poll_data() will get it */ 1005 1006 KBD_DELAY; 1007 data = bus_space_read_1(t->t_iot, t->t_ioh_d, KBDATAP); 1008 1009 if (CMD_IN_QUEUE(q) && pckbc_cmdresponse(t, slot, data)) 1010 continue; 1011 1012 if (sc->inputhandler[slot]) 1013 (*sc->inputhandler[slot])(sc->inputarg[slot], data); 1014 #ifdef PCKBCDEBUG 1015 else 1016 printf("gsckbcintr: slot %d lost %d\n", slot, data); 1017 #endif 1018 } 1019 1020 return (served); 1021 } 1022