#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <machine/autoconf.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <dev/hil/hilreg.h>
#include <dev/hil/hilvar.h>
#include <dev/hil/hildevs.h>
#include <dev/hil/hildevs_data.h>
#include "hilkbd.h"
#define splhil splvm
struct cfdriver hil_cd = {
NULL, "hil", DV_DULL
};
void hilconfig(struct hil_softc *, u_int);
void hilempty(struct hil_softc *);
int hilsubmatch(struct device *, void *, void *);
void hil_process_int(struct hil_softc *, u_int8_t, u_int8_t);
int hil_process_poll(struct hil_softc *, u_int8_t, u_int8_t);
void hil_thread(void *);
int send_device_cmd(struct hil_softc *sc, u_int device, u_int cmd);
void polloff(struct hil_softc *);
void pollon(struct hil_softc *);
static int hilwait(struct hil_softc *);
static int hildatawait(struct hil_softc *);
#define hil_process_pending(sc) wakeup(&(sc)->sc_pending)
static __inline int
hilwait(struct hil_softc *sc)
{
int cnt;
for (cnt = 50000; cnt != 0; cnt--) {
DELAY(1);
if ((bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
HIL_BUSY) == 0)
break;
}
return (cnt);
}
static __inline int
hildatawait(struct hil_softc *sc)
{
int cnt;
for (cnt = 50000; cnt != 0; cnt--) {
DELAY(1);
if ((bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
HIL_DATA_RDY) != 0)
break;
}
return (cnt);
}
void
hil_attach(struct hil_softc *sc, int *hil_is_console)
{
printf("\n");
sc->sc_cmdending = 0;
sc->sc_actdev = sc->sc_cmddev = 0;
sc->sc_cmddone = 0;
sc->sc_cmdbp = sc->sc_cmdbuf;
sc->sc_pollbp = sc->sc_pollbuf;
sc->sc_console = hil_is_console;
}
int
hildevprint(void *aux, const char *pnp)
{
struct hil_attach_args *ha = aux;
if (pnp != NULL) {
printf("\"%s\" at %s id %x",
ha->ha_descr, pnp, ha->ha_id);
}
printf(" code %d", ha->ha_code);
if (pnp == NULL) {
printf(": %s", ha->ha_descr);
}
return (UNCONF);
}
int
hilsubmatch(struct device *parent, void *vcf, void *aux)
{
struct hil_attach_args *ha = aux;
struct cfdata *cf = vcf;
if (cf->cf_loc[0] != -1 &&
cf->cf_loc[0] != ha->ha_code)
return (0);
return ((*cf->cf_attach->ca_match)(parent, vcf, aux));
}
void
hil_attach_deferred(void *v)
{
struct hil_softc *sc = v;
int tries;
u_int8_t db;
sc->sc_status = HIL_STATUS_BUSY;
db = LPC_RECONF | LPC_KBDCOOK | LPC_NOERROR | LPC_AUTOPOLL;
send_hil_cmd(sc, HIL_WRITELPCTRL, &db, 1, NULL);
DELAY(1000000);
if (bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
HIL_DATA_RDY) {
db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
DELAY(1);
}
for (tries = 10; tries != 0; tries--) {
if (send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db) == 0) {
if (db & (LPS_CONFFAIL | LPS_CONFGOOD))
break;
}
#ifdef HILDEBUG
printf("%s: loop not ready, retrying...\n",
sc->sc_dev.dv_xname);
#endif
DELAY(1000000);
}
if (tries == 0 || (db & LPS_CONFFAIL)) {
printf("%s: no devices\n", sc->sc_dev.dv_xname);
sc->sc_pending = 0;
if (tries == 0)
return;
}
if (kthread_create(hil_thread, sc, &sc->sc_thread,
sc->sc_dev.dv_xname) != 0) {
printf("%s: unable to create event thread\n",
sc->sc_dev.dv_xname);
return;
}
send_hil_cmd(sc, HIL_INTON, NULL, 0, NULL);
sc->sc_status = HIL_STATUS_READY;
hil_process_pending(sc);
}
int
hil_intr(void *v)
{
struct hil_softc *sc = v;
u_int8_t c, stat;
if (cold)
return (0);
stat = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
if ((stat & HIL_DATA_RDY) == 0)
return (0);
c = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
HILP_DATA);
DELAY(1);
hil_process_int(sc, stat, c);
if (sc->sc_status != HIL_STATUS_BUSY)
hil_process_pending(sc);
return (1);
}
void
hil_process_int(struct hil_softc *sc, u_int8_t stat, u_int8_t c)
{
struct hildev_softc *dev;
switch ((stat >> HIL_SSHIFT) & HIL_SMASK) {
case HIL_STATUS:
if (c & HIL_ERROR) {
sc->sc_cmddone = 1;
switch (c) {
case HIL_RECONFIG:
sc->sc_pending = HIL_PENDING_RECONFIG;
break;
case HIL_UNPLUGGED:
sc->sc_pending = HIL_PENDING_UNPLUGGED;
break;
}
break;
}
if (c & HIL_COMMAND) {
if (c & HIL_POLLDATA) {
dev = sc->sc_devices[sc->sc_actdev];
if (dev != NULL && dev->sc_fn != NULL)
dev->sc_fn(dev,
sc->sc_pollbp - sc->sc_pollbuf,
sc->sc_pollbuf);
} else {
sc->sc_cmdending = 1;
}
sc->sc_actdev = 0;
} else {
if (c & HIL_POLLDATA) {
sc->sc_actdev = (c & HIL_DEVMASK);
sc->sc_pollbp = sc->sc_pollbuf;
} else {
if (sc->sc_cmddev == (c & HIL_DEVMASK)) {
sc->sc_cmdbp = sc->sc_cmdbuf;
sc->sc_actdev = 0;
}
}
}
break;
case HIL_DATA:
if (sc->sc_actdev != 0)
*sc->sc_pollbp++ = c;
else {
if (sc->sc_cmddev != 0) {
if (sc->sc_cmdending) {
sc->sc_cmddone = 1;
sc->sc_cmdending = 0;
} else
*sc->sc_cmdbp++ = c;
}
}
break;
}
}
int
hil_process_poll(struct hil_softc *sc, u_int8_t stat, u_int8_t c)
{
u_int8_t db;
switch ((stat >> HIL_SSHIFT) & HIL_SMASK) {
case HIL_STATUS:
if (c & HIL_ERROR) {
sc->sc_cmddone = 1;
switch (c) {
case HIL_RECONFIG:
sc->sc_pending = HIL_PENDING_RECONFIG;
db = 0;
send_hil_cmd(sc, HIL_WRITEKBDSADR, &db,
1, NULL);
break;
case HIL_UNPLUGGED:
sc->sc_pending = HIL_PENDING_UNPLUGGED;
break;
}
break;
}
if (c & HIL_COMMAND) {
if (!(c & HIL_POLLDATA)) {
sc->sc_cmdending = 1;
}
sc->sc_actdev = 0;
} else {
if (c & HIL_POLLDATA) {
sc->sc_actdev = (c & HIL_DEVMASK);
sc->sc_pollbp = sc->sc_pollbuf;
} else {
if (sc->sc_cmddev == (c & HIL_DEVMASK)) {
sc->sc_cmdbp = sc->sc_cmdbuf;
sc->sc_actdev = 0;
}
}
}
break;
case HIL_DATA:
if (sc->sc_actdev != 0)
return 1;
else {
if (sc->sc_cmddev != 0) {
if (sc->sc_cmdending) {
sc->sc_cmddone = 1;
sc->sc_cmdending = 0;
}
}
}
break;
}
return 0;
}
void
hil_thread(void *arg)
{
struct hil_softc *sc = arg;
int s;
for (;;) {
s = splhil();
if (sc->sc_pending == 0) {
splx(s);
tsleep_nsec(&sc->sc_pending, PWAIT, "hil_event",
INFSLP);
continue;
}
switch (sc->sc_pending) {
case HIL_PENDING_RECONFIG:
sc->sc_pending = 0;
hilconfig(sc, sc->sc_maxdev);
break;
case HIL_PENDING_UNPLUGGED:
sc->sc_pending = 0;
hilempty(sc);
break;
}
splx(s);
}
}
void
hilconfig(struct hil_softc *sc, u_int knowndevs)
{
struct hil_attach_args ha;
u_int8_t db;
int id, s;
s = splhil();
db = 0;
send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db);
sc->sc_maxdev = db & LPS_DEVMASK;
#ifdef HILDEBUG
printf("%s: %d device(s)\n", sc->sc_dev.dv_xname, sc->sc_maxdev);
#endif
db = 0;
send_hil_cmd(sc, HIL_WRITEKBDSADR, &db, 1, NULL);
for (id = knowndevs + 1; id <= sc->sc_maxdev; id++) {
int len;
const struct hildevice *hd;
if (send_device_cmd(sc, id, HIL_IDENTIFY) != 0) {
printf("%s: no answer from device %d\n",
sc->sc_dev.dv_xname, id);
continue;
}
len = sc->sc_cmdbp - sc->sc_cmdbuf;
if (len == 0) {
#ifdef HILDEBUG
printf("%s: no device at code %d\n",
sc->sc_dev.dv_xname, id);
#endif
continue;
}
for (hd = hildevs; hd->minid >= 0; hd++)
if (sc->sc_cmdbuf[0] >= hd->minid &&
sc->sc_cmdbuf[0] <= hd->maxid) {
ha.ha_console = *sc->sc_console;
ha.ha_code = id;
ha.ha_type = hd->type;
ha.ha_descr = hd->descr;
ha.ha_infolen = len;
bcopy(sc->sc_cmdbuf, ha.ha_info, len);
sc->sc_devices[id] = (struct hildev_softc *)
config_found_sm(&sc->sc_dev, &ha, hildevprint,
hilsubmatch);
#if NHILKBD > 0
if (sc->sc_devices[id] != NULL &&
ha.ha_type == HIL_DEVICE_KEYBOARD &&
ha.ha_console != 0)
*sc->sc_console = 1;
#endif
}
}
for (id = sc->sc_maxdev + 1; id < NHILD; id++) {
if (sc->sc_devices[id] != NULL)
config_detach((struct device *)sc->sc_devices[id],
DETACH_FORCE);
sc->sc_devices[id] = NULL;
}
sc->sc_cmdbp = sc->sc_cmdbuf;
splx(s);
}
void
hilempty(struct hil_softc *sc)
{
u_int8_t db;
int id, s;
u_int oldmaxdev;
s = splhil();
for (;;) {
if (send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db) == 0) {
if (db & (LPS_CONFFAIL | LPS_CONFGOOD))
break;
} else {
db = LPS_CONFFAIL;
break;
}
}
if (db & LPS_CONFFAIL) {
sc->sc_maxdev = 0;
} else {
db = 0;
send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db);
oldmaxdev = sc->sc_maxdev;
sc->sc_maxdev = db & LPS_DEVMASK;
if (sc->sc_maxdev != 0) {
hilconfig(sc, oldmaxdev);
splx(s);
return;
}
}
for (id = sc->sc_maxdev + 1; id < NHILD; id++) {
if (sc->sc_devices[id] != NULL)
config_detach((struct device *)sc->sc_devices[id],
DETACH_FORCE);
sc->sc_devices[id] = NULL;
}
sc->sc_cmdbp = sc->sc_cmdbuf;
splx(s);
}
int
send_hil_cmd(struct hil_softc *sc, u_int cmd, u_int8_t *data, u_int dlen,
u_int8_t *rdata)
{
u_int8_t status;
int s;
s = splhil();
if (hilwait(sc) == 0) {
#ifdef HILDEBUG
printf("%s: no answer from the loop\n", sc->sc_dev.dv_xname);
#endif
splx(s);
return (EBUSY);
}
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, cmd);
while (dlen--) {
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, *data++);
DELAY(1);
}
if (rdata) {
do {
if (hildatawait(sc) == 0) {
#ifdef HILDEBUG
printf("%s: no answer from the loop\n",
sc->sc_dev.dv_xname);
#endif
break;
}
status = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
HILP_STAT);
*rdata = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
HILP_DATA);
DELAY(1);
} while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K);
}
splx(s);
return (0);
}
int
send_device_cmd(struct hil_softc *sc, u_int device, u_int cmd)
{
u_int8_t status, c;
int rc = 0;
polloff(sc);
sc->sc_cmdbp = sc->sc_cmdbuf;
sc->sc_cmddev = device;
if (hilwait(sc) == 0) {
#ifdef HILDEBUG
printf("%s: no answer from device %d\n",
sc->sc_dev.dv_xname, device);
#endif
rc = EBUSY;
goto out;
}
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_STARTCMD);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 8 + device);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, cmd);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, HIL_TIMEOUT);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_TRIGGER);
sc->sc_cmddone = 0;
do {
if (hildatawait(sc) == 0) {
#ifdef HILDEBUG
printf("%s: no answer from device %d\n",
sc->sc_dev.dv_xname, device);
#endif
rc = EBUSY;
break;
}
status = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
c = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
DELAY(1);
hil_process_int(sc, status, c);
} while (sc->sc_cmddone == 0);
out:
sc->sc_cmddev = 0;
pollon(sc);
return (rc);
}
int
send_hildev_cmd(struct hildev_softc *dev, u_int cmd,
u_int8_t *outbuf, u_int *outlen)
{
struct hil_softc *sc = (struct hil_softc *)dev->sc_dev.dv_parent;
int s, rc;
s = splhil();
if ((rc = send_device_cmd(sc, dev->sc_code, cmd)) == 0) {
if (outbuf != NULL && outlen != NULL) {
*outlen = min(*outlen, sc->sc_cmdbp - sc->sc_cmdbuf);
bcopy(sc->sc_cmdbuf, outbuf, *outlen);
}
}
splx(s);
return (rc);
}
void
polloff(struct hil_softc *sc)
{
u_int8_t db;
if (hilwait(sc) == 0)
return;
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_SETARR);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 0);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READLPCTRL);
hildatawait(sc);
db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
db &= ~LPC_AUTOPOLL;
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_WRITELPCTRL);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, db);
do {
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READBUSY);
hildatawait(sc);
db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
} while (db & BSY_LOOPBUSY);
sc->sc_cmddone = 0;
sc->sc_cmddev = 0;
}
void
pollon(struct hil_softc *sc)
{
u_int8_t db;
if (hilwait(sc) == 0)
return;
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READLPCTRL);
hildatawait(sc);
db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
db |= LPC_AUTOPOLL;
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_WRITELPCTRL);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, db);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_SETARR);
hilwait(sc);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 0);
DELAY(1);
}
void
hil_set_poll(struct hil_softc *sc, int on)
{
if (on) {
pollon(sc);
} else {
hil_process_pending(sc);
send_hil_cmd(sc, HIL_INTON, NULL, 0, NULL);
}
}
int
hil_poll_data(struct hildev_softc *dev, u_int8_t *stat, u_int8_t *data)
{
struct hil_softc *sc = (struct hil_softc *)dev->sc_dev.dv_parent;
u_int8_t s, c;
s = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
if ((s & HIL_DATA_RDY) == 0)
return -1;
c = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
DELAY(1);
if (hil_process_poll(sc, s, c)) {
if (sc->sc_actdev == dev->sc_code) {
*stat = s;
*data = c;
return 0;
}
}
return -1;
}