#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cs80bus.c,v 1.20 2026/03/01 20:46:00 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/endian.h>
#include <sys/malloc.h>
#include <dev/gpib/gpibvar.h>
#include <dev/gpib/cs80busvar.h>
#ifndef DEBUG
#define DEBUG
#endif
#ifdef DEBUG
int cs80busdebug = 0xff;
#define DBG_FOLLOW 0x01
#define DBG_STATUS 0x02
#define DBG_FAIL 0x04
#define DPRINTF(mask, str) if (cs80busdebug & (mask)) printf str
#else
#define DPRINTF(mask, str)
#endif
#include "locators.h"
#define cs80buscf_slave cf_loc[CS80BUSCF_SLAVE]
#define cs80buscf_punit cf_loc[CS80BUSCF_PUNIT]
int cs80busmatch(device_t, cfdata_t, void *);
void cs80busattach(device_t, device_t, void *);
CFATTACH_DECL_NEW(cs80bus, sizeof(struct cs80bus_softc),
cs80busmatch, cs80busattach, NULL, NULL);
static int cs80bus_alloc(struct cs80bus_softc *, int, int);
static int cs80bussearch(device_t, cfdata_t,
const int *, void *);
static int cs80busprint(void *, const char *);
int
cs80busmatch(device_t parent, cfdata_t match, void *aux)
{
return (1);
}
void
cs80busattach(device_t parent, device_t self, void *aux)
{
struct cs80bus_softc *sc = device_private(self);
struct gpib_attach_args *ga = aux;
struct cs80bus_attach_args ca;
int slave;
u_int16_t id;
printf("\n");
sc->sc_dev = self;
sc->sc_ic = ga->ga_ic;
for (slave = 0; slave < 8; slave++) {
if (gpib_isalloc(device_private(device_parent(sc->sc_dev)),
slave))
continue;
if (gpibrecv(sc->sc_ic, GPIB_BROADCAST_ADDR,
slave, &id, 2) != 2)
continue;
BE16TOH(id);
DPRINTF(DBG_STATUS, ("cs80busattach: found id 0x%x\n", id));
if ((id & 0x200) == 0)
continue;
ca.ca_ic = sc->sc_ic;
ca.ca_slave = slave;
ca.ca_id = id;
config_search(sc->sc_dev, &ca,
CFARGS(.search = cs80bussearch));
}
}
int
cs80bussearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct cs80bus_softc *sc = device_private(parent);
struct cs80bus_attach_args *ca = aux;
if (cf->cs80buscf_punit != CS80BUSCF_PUNIT_DEFAULT &&
cf->cs80buscf_punit < CS80BUS_NPUNITS)
ca->ca_punit = cf->cs80buscf_punit;
else
ca->ca_punit = 0;
DPRINTF(DBG_FOLLOW, ("cs80bussearch: id=0x%x slave=%d punit=%d\n",
ca->ca_id, ca->ca_slave, ca->ca_punit));
if (config_probe(parent, cf, ca)) {
DPRINTF(DBG_FOLLOW,
("cs80bussearch: got id=0x%x slave=%d punit %d\n",
ca->ca_id, ca->ca_slave, ca->ca_punit));
if (cf->cs80buscf_slave != CS80BUSCF_SLAVE_DEFAULT &&
cf->cs80buscf_slave != ca->ca_slave)
goto out;
if (cf->cs80buscf_punit != CS80BUSCF_PUNIT_DEFAULT &&
cf->cs80buscf_punit != ca->ca_punit)
goto out;
if (cs80bus_alloc(sc, ca->ca_slave, ca->ca_punit))
goto out;
config_attach(parent, cf, ca, cs80busprint, CFARGS_NONE);
}
out:
return (0);
}
int
cs80busprint(void *aux, const char *pnp)
{
struct cs80bus_attach_args *ca = aux;
printf(" slave %d punit %d", ca->ca_slave, ca->ca_punit);
return (UNCONF);
}
static int
cs80bus_alloc(struct cs80bus_softc *sc, int slave, int punit)
{
DPRINTF(DBG_FOLLOW, ("cs80bus_alloc: sc=%p\n", sc));
if (slave >= CS80BUS_NSLAVES || punit >= CS80BUS_NPUNITS)
panic("cs80bus_alloc: device address out of range");
gpib_alloc(device_private(device_parent(sc->sc_dev)), slave);
if (sc->sc_rmap[slave][punit] == 0) {
sc->sc_rmap[slave][punit] = 1;
return (0);
}
return (1);
}
int
cs80describe(void *v, int slave, int punit, struct cs80_description *csd)
{
struct cs80bus_softc *sc = v;
struct cs80_describecmd desc;
u_int8_t stat;
DPRINTF(DBG_FOLLOW, ("cs80describe: sc=%p slave=%d\n", sc, slave));
desc.c_unit = CS80CMD_SUNIT(0);
desc.c_vol = CS80CMD_SVOL(0);
desc.c_cmd = CS80CMD_DESC;
(void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &desc, sizeof(desc));
(void) gpibrecv(sc->sc_ic, slave, CS80CMD_EXEC, csd, sizeof(*csd));
(void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
if (stat != 0) {
DPRINTF(DBG_FAIL, ("cs80describe: failed, stat=0x%x\n", stat));
return (1);
}
BE16TOH(csd->d_iuw);
BE16TOH(csd->d_cmaxxfr);
BE16TOH(csd->d_sectsize);
BE16TOH(csd->d_blocktime);
BE16TOH(csd->d_uavexfr);
BE16TOH(csd->d_retry);
BE16TOH(csd->d_access);
BE32TOH(csd->d_maxcylhead);
BE16TOH(csd->d_maxsect);
BE16TOH(csd->d_maxvsecth);
BE32TOH(csd->d_maxvsectl);
return (0);
}
int
cs80reset(void *v, int slave, int punit)
{
struct cs80bus_softc *sc = v;
struct cs80_clearcmd clear;
struct cs80_srcmd sr;
struct cs80_ssmcmd ssm;
DPRINTF(DBG_FOLLOW, ("cs80reset: sc=%p slave=%d punit=%d\n", sc,
slave, punit));
clear.c_unit = CS80CMD_SUNIT(punit);
clear.c_cmd = CS80CMD_CLEAR;
if (cs80send(sc, slave, punit, CS80CMD_TCMD, &clear, sizeof(clear))) {
DPRINTF(DBG_FAIL, ("cs80reset: CLEAR failed\n"));
return (1);
}
sr.c_unit = CS80CMD_SUNIT(15);
sr.c_nop = CS80CMD_NOP;
sr.c_cmd = CS80CMD_SREL;
sr.c_param = 0xc0;
if (cs80send(sc, slave, punit, CS80CMD_SCMD, &sr, sizeof(sr))) {
DPRINTF(DBG_FAIL, ("cs80reset: SREL failed\n"));
return (1);
}
ssm.c_unit = CS80CMD_SUNIT(punit);
ssm.c_cmd = CS80CMD_SSM;
ssm.c_refm = htobe16(REF_MASK);
ssm.c_fefm = htobe16(FEF_MASK);
ssm.c_aefm = htobe16(AEF_MASK);
ssm.c_iefm = htobe16(IEF_MASK);
if (cs80send(sc, slave, punit, CS80CMD_SCMD, &ssm, sizeof(ssm))) {
DPRINTF(DBG_FAIL, ("cs80reset: SSM failed\n"));
return (1);
}
return (0);
}
int
cs80status(void *v, int slave, int punit, struct cs80_stat *css)
{
struct cs80bus_softc *sc = v;
struct cs80_statuscmd rs;
u_int8_t stat;
rs.c_unit = CS80CMD_SUNIT(punit);
rs.c_sram = CS80CMD_SRAM;
rs.c_param = 0;
rs.c_cmd = CS80CMD_STATUS;
memset((void *)css, 0, sizeof(*css));
(void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &rs, sizeof(rs));
(void) gpibrecv(sc->sc_ic, slave, CS80CMD_EXEC, css, sizeof(*css));
(void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
if (stat != 0) {
DPRINTF(DBG_FAIL, ("cs80status: failed, stat=0x%x\n", stat));
return (1);
}
BE16TOH(css->c_ref);
BE16TOH(css->c_fef);
BE16TOH(css->c_aef);
BE16TOH(css->c_ief);
BE32TOH(css->c_blk);
return (0);
}
int
cs80setoptions(void *v, int slave, int punit, u_int8_t options)
{
struct cs80bus_softc *sc = v;
struct cs80_soptcmd opt;
opt.c_unit = CS80CMD_SUNIT(punit);
opt.c_nop = CS80CMD_NOP;
opt.c_opt = CS80CMD_SOPT;
opt.c_param = options;
if (cs80send(sc, slave, punit, CS80CMD_SCMD, &opt, sizeof(opt))) {
DPRINTF(DBG_FAIL, ("cs80setoptions: failed\n"));
return (1);
}
return (0);
}
int
cs80send(void *v, int slave, int punit, int cmd, void *ptr, int cnt)
{
struct cs80bus_softc *sc = v;
u_int8_t *buf = ptr;
u_int8_t stat;
DPRINTF(DBG_FOLLOW,
("cs80send: sc=%p slave=%d punit=%d cmd=%d ptr=%p cnt=%d\n", sc,
slave, punit, cmd, buf, cnt));
if (gpibsend(sc->sc_ic, slave, cmd, buf, cnt) != cnt) {
DPRINTF(DBG_FAIL, ("cs80send: SCMD failed\n"));
return (1);
}
if (gpibswait(sc->sc_ic, slave)) {
DPRINTF(DBG_FAIL, ("cs80send: wait failed\n"));
return (1);
}
if (gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1) != 1) {
DPRINTF(DBG_FAIL, ("cs80send: QSTAT failed\n"));
return (1);
}
if (stat != 0) {
DPRINTF(DBG_FAIL, ("cs80send: failed, stat=0x%x\n", stat));
return (1);
}
return (0);
}