#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <machine/autoconf.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"
static void hilconfig(struct hil_softc *, u_int);
static void hilempty(struct hil_softc *);
static int hilsubmatch(device_t, cfdata_t, const int *, void *);
static void hil_process_int(struct hil_softc *, uint8_t, uint8_t);
static int hil_process_poll(struct hil_softc *, uint8_t, uint8_t);
static void hil_thread(void *);
static int send_device_cmd(struct hil_softc *sc, u_int device, u_int cmd);
static void polloff(struct hil_softc *);
static 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)
{
rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
RND_TYPE_TTY, RND_FLAG_DEFAULT);
aprint_normal("\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;
sc->sc_status = HIL_STATUS_BUSY;
}
int
hildevprint(void *aux, const char *pnp)
{
struct hil_attach_args *ha = aux;
if (pnp != NULL) {
aprint_normal("\"%s\" at %s id %x",
ha->ha_descr, pnp, ha->ha_id);
}
aprint_normal(" code %d", ha->ha_code);
if (pnp == NULL) {
aprint_normal(": %s", ha->ha_descr);
}
return UNCONF;
}
int
hilsubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct hil_attach_args *ha = aux;
if (cf->cf_loc[0] != -1 &&
cf->cf_loc[0] != ha->ha_code)
return 0;
return config_match(parent, cf, aux);
}
void
hil_attach_deferred(device_t self)
{
struct hil_softc *sc = device_private(self);
int tries;
uint8_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
aprint_debug(self, "%s: loop not ready, retrying...\n");
#endif
DELAY(1000000);
}
if (tries == 0 || (db & LPS_CONFFAIL)) {
aprint_normal_dev(self, "no devices\n");
sc->sc_pending = 0;
if (tries == 0)
return;
}
if (kthread_create(PRI_NONE, 0, NULL, hil_thread, sc, &sc->sc_thread,
"%s", device_xname(sc->sc_dev)) != 0) {
aprint_error_dev(self, "unable to create event thread\n");
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;
uint8_t c, stat;
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);
rnd_add_uint32(&sc->sc_rndsource, (stat << 8) | c);
return 1;
}
void
hil_process_int(struct hil_softc *sc, uint8_t stat, uint8_t c)
{
device_t child;
struct hildev_softc *hdsc;
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) {
child = sc->sc_devices[sc->sc_actdev];
if (child != NULL) {
hdsc = device_private(child);
if (hdsc->sc_fn != NULL)
(*hdsc->sc_fn)(hdsc,
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, uint8_t stat, uint8_t c)
{
uint8_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);
(void)tsleep(&sc->sc_pending, PWAIT, "hil_event", 0);
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;
uint8_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", device_xname(sc->sc_dev), 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) {
aprint_normal_dev(sc->sc_dev,
"no answer from device %d\n", id);
continue;
}
len = sc->sc_cmdbp - sc->sc_cmdbuf;
if (len == 0) {
#ifdef HILDEBUG
printf("%s: no device at code %d\n",
device_xname(sc->sc_dev), 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;
memcpy(ha.ha_info, sc->sc_cmdbuf, len);
sc->sc_devices[id] =
config_found(sc->sc_dev, &ha, hildevprint,
CFARGS(.submatch = 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(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)
{
uint8_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);
return;
}
}
for (id = sc->sc_maxdev + 1; id < NHILD; id++) {
if (sc->sc_devices[id] != NULL)
config_detach(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, uint8_t *data, u_int dlen,
uint8_t *rdata)
{
uint8_t status;
int s;
s = splhil();
if (hilwait(sc) == 0) {
#ifdef HILDEBUG
printf("%s: no answer from the loop\n",
device_xname(sc->sc_dev));
#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",
device_xname(sc->sc_dev));
#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)
{
uint8_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",
device_xname(sc->sc_dev), 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",
device_xname(sc->sc_dev), 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 *hdsc, u_int cmd,
uint8_t *outbuf, u_int *outlen)
{
struct hil_softc *sc = device_private(device_parent(hdsc->sc_dev));
int s, rc;
s = splhil();
if ((rc = send_device_cmd(sc, hdsc->sc_code, cmd)) == 0) {
if (outbuf != NULL && outlen != NULL) {
*outlen = uimin(*outlen, sc->sc_cmdbp - sc->sc_cmdbuf);
memcpy(outbuf, sc->sc_cmdbuf, *outlen);
}
}
splx(s);
return rc;
}
void
polloff(struct hil_softc *sc)
{
uint8_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)
{
uint8_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 *hdsc, uint8_t *stat, uint8_t *data)
{
struct hil_softc *sc = device_private(device_parent(hdsc->sc_dev));
uint8_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 == hdsc->sc_code) {
*stat = s;
*data = c;
return 0;
}
}
return -1;
}