/* * Reset the keyboard controller in a violent fashion; normally done * after suspend/resume when we do not trust the machine. */ void pckbc_reset(struct pckbc_softc *sc) { struct pckbc_internal *t = sc->id; bus_space_tag_t iot = t->t_iot; bus_space_handle_t ioh_d = t->t_ioh_d, ioh_c = t->t_ioh_c; pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0); /* KBC selftest */ if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) return; pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0); (void)pckbc_put8042cmd(t); pckbcintr_internal(t->t_sc->id, t->t_sc); }
bool pckbc_resume(device_t dv, const pmf_qual_t *qual) { struct pckbc_softc *sc = device_private(dv); struct pckbc_internal *t; t = sc->id; (void)pckbc_poll_data1(t, PCKBC_KBD_SLOT); if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c, KBC_SELFTEST)) return false; (void)pckbc_poll_data1(t, PCKBC_KBD_SLOT); (void)pckbc_put8042cmd(t); pckbcintr(t->t_sc); return true; }
void pckbc_flush(pckbc_tag_t self, pckbc_slot_t slot) { struct pckbc_internal *t = self; (void) pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c, slot, t->t_haveaux); }
int pckbc_cnattach(bus_space_tag_t iot, bus_addr_t addr, bus_size_t cmd_offset, int flags) { bus_space_handle_t ioh_d, ioh_c; int res = 0; if (bus_space_map(iot, addr + KBDATAP, 1, 0, &ioh_d)) return (ENXIO); if (bus_space_map(iot, addr + cmd_offset, 1, 0, &ioh_c)) { bus_space_unmap(iot, ioh_d, 1); return (ENXIO); } pckbc_consdata.t_iot = iot; pckbc_consdata.t_ioh_d = ioh_d; pckbc_consdata.t_ioh_c = ioh_c; pckbc_consdata.t_addr = addr; pckbc_consdata.t_flags = flags; timeout_set(&pckbc_consdata.t_cleanup, pckbc_cleanup, &pckbc_consdata); timeout_set(&pckbc_consdata.t_poll, pckbc_poll, &pckbc_consdata); /* flush */ (void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0); /* selftest? */ /* init cmd byte, enable ports */ pckbc_consdata.t_cmdbyte = KC8_CPU; if (!pckbc_put8042cmd(&pckbc_consdata)) { printf("kbc: cmd word write error\n"); res = EIO; } if (!res) { #if (NPCKBD > 0) res = pckbd_cnattach(&pckbc_consdata); #else res = ENXIO; #endif /* NPCKBD > 0 */ } if (res) { bus_space_unmap(iot, pckbc_consdata.t_ioh_d, 1); bus_space_unmap(iot, pckbc_consdata.t_ioh_c, 1); } else { pckbc_consdata.t_slotdata[PCKBC_KBD_SLOT] = &pckbc_cons_slotdata; pckbc_init_slotdata(&pckbc_cons_slotdata); pckbc_console = 1; } return (res); }
/* * Get the current command byte. */ static int pckbc_get8042cmd(struct pckbc_internal *t) { bus_space_tag_t iot = t->t_iot; bus_space_handle_t ioh_c = t->t_ioh_c; int data; if (!pckbc_send_cmd(iot, ioh_c, K_RDCMDBYTE)) return (0); data = pckbc_poll_data1(t, PCKBC_KBD_SLOT); if (data == -1) return (0); t->t_cmdbyte = data; return (1); }
int pckbc_poll_data(pckbc_tag_t self, pckbc_slot_t slot) { struct pckbc_internal *t = self; struct pckbc_slotdata *q = t->t_slotdata[slot]; int c; c = pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c, slot, t->t_haveaux); if (c != -1 && q && CMD_IN_QUEUE(q)) { /* we jumped into a running command - try to deliver the response */ if (pckbc_cmdresponse(t, slot, c)) return (-1); } return (c); }
/* * Pass command to device, poll for ACK and data. * to be called at spltty() */ static void pckbc_poll_cmd1(struct pckbc_internal *t, pckbc_slot_t slot, struct pckbc_devcmd *cmd) { bus_space_tag_t iot = t->t_iot; bus_space_handle_t ioh_d = t->t_ioh_d; bus_space_handle_t ioh_c = t->t_ioh_c; int i, c = 0; while (cmd->cmdidx < cmd->cmdlen) { if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) { printf("pckbc_cmd: send error\n"); cmd->status = EIO; return; } for (i = 10; i; i--) { /* 1s ??? */ c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot, t->t_haveaux); if (c != -1) break; } switch (c) { case KBC_DEVCMD_ACK: cmd->cmdidx++; continue; /* * Some legacy free PCs keep returning Basic Assurance Test * (BAT) instead of something usable, so fail gracefully. */ case KBC_DEVCMD_RESEND: case KBC_DEVCMD_BAT_DONE: case KBC_DEVCMD_BAT_FAIL: DPRINTF("pckbc_cmd: %s\n", c == KBC_DEVCMD_RESEND ? "RESEND": "BAT"); if (cmd->retries++ < 5) continue; DPRINTF("pckbc_cmd: cmd failed\n"); cmd->status = ENXIO; return; case -1: DPRINTF("pckbc_cmd: timeout\n"); cmd->status = EIO; return; default: DPRINTF("pckbc_cmd: lost 0x%x\n", c); } } while (cmd->responseidx < cmd->responselen) { if (cmd->flags & KBC_CMDFLAG_SLOW) i = 100; /* 10s ??? */ else i = 10; /* 1s ??? */ while (i--) { c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot, t->t_haveaux); if (c != -1) break; } if (c == -1) { DPRINTF("pckbc_cmd: no data\n"); cmd->status = ETIMEDOUT; return; } else cmd->response[cmd->responseidx++] = c; } }
void pckbc_attach(struct pckbc_softc *sc, int flags) { struct pckbc_internal *t; bus_space_tag_t iot; bus_space_handle_t ioh_d, ioh_c; int haskbd = 0, res; u_char cmdbits = 0; t = sc->id; iot = t->t_iot; ioh_d = t->t_ioh_d; ioh_c = t->t_ioh_c; if (pckbc_console == 0) { timeout_set(&t->t_cleanup, pckbc_cleanup, t); timeout_set(&t->t_poll, pckbc_poll, t); } /* flush */ (void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0); /* set initial cmd byte */ if (!pckbc_put8042cmd(t)) { #if defined(__i386__) || defined(__amd64__) if (!ISSET(flags, PCKBCF_FORCE_KEYBOARD_PRESENT)) { pckbc_release_console(); return; } #endif printf("kbc: cmd word write error\n"); return; } /* * XXX Don't check the keyboard port. There are broken keyboard controllers * which don't pass the test but work normally otherwise. */ #if 0 /* * check kbd port ok */ if (!pckbc_send_cmd(iot, ioh_c, KBC_KBDTEST)) return; res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0); /* * Normally, we should get a "0" here. * But there are keyboard controllers behaving differently. */ if (res == 0 || res == 0xfa || res == 0x01 || res == 0xab) { #ifdef PCKBCDEBUG if (res != 0) printf("kbc: returned %x on kbd slot test\n", res); #endif if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT, 0)) { cmdbits |= KC8_KENABLE; haskbd = 1; } } else { printf("kbc: kbd port test: %x\n", res); return; } #else if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT, 0)) { cmdbits |= KC8_KENABLE; haskbd = 1; } #endif /* 0 */ /* * Check aux port ok. * Avoid KBC_AUXTEST because it hangs some older controllers * (eg UMC880?). */ if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXECHO)) { printf("kbc: aux echo error 1\n"); goto nomouse; } if (!pckbc_wait_output(iot, ioh_c)) { printf("kbc: aux echo error 2\n"); goto nomouse; } bus_space_write_1(iot, ioh_d, 0, 0x5a); /* a random value */ res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_AUX_SLOT, 1); if (ISSET(t->t_flags, PCKBC_NEED_AUXWRITE)) { /* * The following code is necessary to find the aux port on the * oqo-1 machine, among others. However if confuses old * (non-ps/2) keyboard controllers (at least UMC880x again). */ if (res == -1) { /* Read of aux echo timed out, try again */ if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE)) goto nomouse; if (!pckbc_wait_output(iot, ioh_c)) goto nomouse; bus_space_write_1(iot, ioh_d, 0, 0x5a); res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_AUX_SLOT, 1); DPRINTF("kbc: aux echo: %x\n", res); } } if (res != -1) { /* * In most cases, the 0x5a gets echoed. * Some old controllers (Gateway 2000 circa 1993) * return 0xfe here. * We are satisfied if there is anything in the * aux output buffer. */ DPRINTF("kbc: aux echo: %x\n", res); t->t_haveaux = 1; if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT, 0)) cmdbits |= KC8_MENABLE; } #ifdef PCKBCDEBUG else printf("kbc: aux echo test failed\n"); #endif #if defined(__i386__) || defined(__amd64__) if (haskbd == 0 && !ISSET(flags, PCKBCF_FORCE_KEYBOARD_PRESENT)) { if (t->t_haveaux) { if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT, 1)) cmdbits |= KC8_KENABLE; } else { pckbc_release_console(); } } #endif nomouse: /* enable needed interrupts */ t->t_cmdbyte |= cmdbits; if (!pckbc_put8042cmd(t)) printf("kbc: cmd word write error\n"); }
static void pckbc_js_attach_common(struct pckbc_js_softc *jsc, bus_space_tag_t iot, bus_addr_t ioaddr, int intr, int isconsole) { struct pckbc_softc *sc = (struct pckbc_softc *)jsc; struct pckbc_internal *t; jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish; jsc->jsc_intr = intr; jsc->jsc_establised = 0; if (isconsole) { int status; status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT, 0); if (status == 0) aprint_normal(": cnattach ok"); else aprint_error(": cnattach %d", status); } if (pckbc_is_console(iot, ioaddr)) { t = &pckbc_consdata; pckbc_console_attached = 1; } else { bus_space_handle_t ioh_d, ioh_c; if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) { aprint_error(": unable to map data register\n"); return; } if (bus_space_map(iot, ioaddr + KBCMDP, 1, 0, &ioh_c) != 0) { bus_space_unmap(iot, ioh_d, 1); aprint_error(": unable to map cmd register\n"); return; } t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK); memset(t, 0, sizeof(struct pckbc_internal)); t->t_iot = iot; t->t_ioh_d = ioh_d; t->t_ioh_c = ioh_c; t->t_addr = ioaddr; t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */ callout_init(&t->t_cleanup, 0); (void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */ if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) aprint_error(": unable to request self test"); else { int response; response = pckbc_poll_data1(t, PCKBC_KBD_SLOT); if (response == 0x55) aprint_normal(": selftest ok"); else aprint_error(": selftest failed (0x%02x)", response); } } /* crosslink */ t->t_sc = sc; sc->id = t; /* finish off the attach */ aprint_normal("\n"); pckbc_attach(sc); }
int pckbc_cnattach(bus_space_tag_t iot, bus_addr_t addr, bus_size_t cmd_offset, pckbc_slot_t slot, int flags) { bus_space_handle_t ioh_d, ioh_c; #ifdef PCKBC_CNATTACH_SELFTEST int reply; #endif int res = 0; if (bus_space_map(iot, addr + KBDATAP, 1, 0, &ioh_d)) return (ENXIO); if (bus_space_map(iot, addr + cmd_offset, 1, 0, &ioh_c)) { bus_space_unmap(iot, ioh_d, 1); return (ENXIO); } memset(&pckbc_consdata, 0, sizeof(pckbc_consdata)); pckbc_consdata.t_iot = iot; pckbc_consdata.t_ioh_d = ioh_d; pckbc_consdata.t_ioh_c = ioh_c; pckbc_consdata.t_addr = addr; pckbc_consdata.t_flags = flags; callout_init(&pckbc_consdata.t_cleanup, 0); /* flush */ (void) pckbc_poll_data1(&pckbc_consdata, PCKBC_KBD_SLOT); #ifdef PCKBC_CNATTACH_SELFTEST /* * In some machines (e.g. netwinder) pckbc refuses to talk at * all until we request a self-test. */ if (!pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST)) { printf("pckbc: unable to request selftest\n"); res = EIO; goto out; } reply = pckbc_poll_data1(&pckbc_consdata, PCKBC_KBD_SLOT); if (reply != 0x55) { printf("pckbc: selftest returned 0x%02x\n", reply); res = EIO; goto out; } #endif /* PCKBC_CNATTACH_SELFTEST */ /* init cmd byte, enable ports */ pckbc_consdata.t_cmdbyte = KC8_CPU; if (!pckbc_put8042cmd(&pckbc_consdata)) { printf("pckbc: cmd word write error\n"); res = EIO; goto out; } res = pckbport_cnattach(&pckbc_consdata, &pckbc_ops, slot); out: if (res) { bus_space_unmap(iot, pckbc_consdata.t_ioh_d, 1); bus_space_unmap(iot, pckbc_consdata.t_ioh_c, 1); } else { pckbc_consdata.t_slotdata[slot] = &pckbc_cons_slotdata; pckbc_init_slotdata(&pckbc_cons_slotdata); pckbc_console = 1; } return (res); }
void pckbc_attach(struct pckbc_softc *sc) { struct pckbc_internal *t; bus_space_tag_t iot; bus_space_handle_t ioh_d, ioh_c; int res; u_char cmdbits = 0; t = sc->id; iot = t->t_iot; ioh_d = t->t_ioh_d; ioh_c = t->t_ioh_c; t->t_pt = pckbport_attach(t, &pckbc_ops); if (t->t_pt == NULL) { aprint_error(": attach failed\n"); return; } /* flush */ (void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* set initial cmd byte */ if (!pckbc_put8042cmd(t)) { printf("pckbc: cmd word write error\n"); return; } /* * XXX Don't check the keyboard port. There are broken keyboard controllers * which don't pass the test but work normally otherwise. */ #if 0 /* * check kbd port ok */ if (!pckbc_send_cmd(iot, ioh_c, KBC_KBDTEST)) return; res = pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0); /* * Normally, we should get a "0" here. * But there are keyboard controllers behaving differently. */ if (res == 0 || res == 0xfa || res == 0x01 || res == 0xab) { #ifdef PCKBCDEBUG if (res != 0) printf("pckbc: returned %x on kbd slot test\n", res); #endif if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT)) cmdbits |= KC8_KENABLE; } else { printf("pckbc: kbd port test: %x\n", res); return; } #else if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT)) cmdbits |= KC8_KENABLE; #endif /* 0 */ /* * Check aux port ok. * Avoid KBC_AUXTEST because it hangs some older controllers * (eg UMC880?). */ if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXECHO)) { printf("pckbc: aux echo error 1\n"); goto nomouse; } if (!pckbc_wait_output(iot, ioh_c)) { printf("pckbc: aux echo error 2\n"); goto nomouse; } t->t_haveaux = 1; bus_space_write_1(iot, ioh_d, 0, 0x5a); /* a random value */ res = pckbc_poll_data1(t, PCKBC_AUX_SLOT); /* * The following is needed to find the aux port on the Tadpole * SPARCle. */ if (res == -1 && ISSET(t->t_flags, PCKBC_NEED_AUXWRITE)) { /* Read of aux echo timed out, try again */ if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE)) goto nomouse; if (!pckbc_wait_output(iot, ioh_c)) goto nomouse; bus_space_write_1(iot, ioh_d, 0, 0x5a); res = pckbc_poll_data1(t, PCKBC_AUX_SLOT); } if (res != -1) { /* * In most cases, the 0x5a gets echoed. * Some older controllers (Gateway 2000 circa 1993) * return 0xfe here. * We are satisfied if there is anything in the * aux output buffer. */ if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT)) cmdbits |= KC8_MENABLE; } else { #ifdef PCKBCDEBUG printf("pckbc: aux echo test failed\n"); #endif t->t_haveaux = 0; } nomouse: /* enable needed interrupts */ t->t_cmdbyte |= cmdbits; if (!pckbc_put8042cmd(t)) printf("pckbc: cmd word write error\n"); }
int pckbc_isa_match(device_t parent, cfdata_t match, void *aux) { struct isa_attach_args *ia = aux; bus_space_tag_t iot = ia->ia_iot; bus_space_handle_t ioh_d, ioh_c; int res, ok = 1; if (ISA_DIRECT_CONFIG(ia)) return (0); /* If values are hardwired to something that they can't be, punt. */ if (ia->ia_nio < 1 || (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT && ia->ia_io[0].ir_addr != IO_KBD)) return (0); if (ia->ia_niomem > 0 && (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM)) return (0); if (ia->ia_nirq < 1 || (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ && ia->ia_irq[0].ir_irq != 1 /*XXX*/)) return (0); if (ia->ia_ndrq > 0 && (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ)) return (0); if (pckbc_is_console(iot, IO_KBD) == 0) { struct pckbc_internal t; if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d)) return (0); if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) { bus_space_unmap(iot, ioh_d, 1); return (0); } memset(&t, 0, sizeof(t)); t.t_iot = iot; t.t_ioh_d = ioh_d; t.t_ioh_c = ioh_c; /* flush KBC */ (void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT); /* KBC selftest */ if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) { ok = 0; goto out; } res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT); #ifndef PCKBCNOTEST if (res != 0x55) { #ifdef PCKBCDEBUG aprint_verbose("kbc selftest: %x\n", res); #endif ok = 0; } #endif /* PCKBCNOTEST */ out: bus_space_unmap(iot, ioh_d, 1); bus_space_unmap(iot, ioh_c, 1); } if (ok) { ia->ia_io[0].ir_addr = IO_KBD; ia->ia_io[0].ir_size = 5; ia->ia_nio = 1; ia->ia_niomem = 0; ia->ia_nirq = 0; ia->ia_ndrq = 0; } return (ok); }