#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ualea.c,v 1.19 2022/03/20 13:18:30 riastradh Exp $");
#include <sys/types.h>
#include <sys/atomic.h>
#include <sys/device_if.h>
#include <sys/kmem.h>
#include <sys/module.h>
#include <sys/rndsource.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
struct ualea_softc {
device_t sc_dev;
kmutex_t sc_lock;
krndsource_t sc_rnd;
uint16_t sc_maxpktsize;
struct usbd_pipe *sc_pipe;
struct usbd_xfer *sc_xfer;
size_t sc_needed;
bool sc_attached:1;
bool sc_inflight:1;
};
static int ualea_match(device_t, cfdata_t, void *);
static void ualea_attach(device_t, device_t, void *);
static int ualea_detach(device_t, int);
static void ualea_get(size_t, void *);
static void ualea_xfer_done(struct usbd_xfer *, void *, usbd_status);
CFATTACH_DECL_NEW(ualea, sizeof(struct ualea_softc),
ualea_match, ualea_attach, ualea_detach, NULL);
static const struct usb_devno ualea_devs[] = {
{ USB_VENDOR_ARANEUS, USB_PRODUCT_ARANEUS_ALEA },
};
static int
ualea_match(device_t parent, cfdata_t match, void *aux)
{
struct usbif_attach_arg *uiaa = aux;
if (usb_lookup(ualea_devs, uiaa->uiaa_vendor, uiaa->uiaa_product))
return UMATCH_VENDOR_PRODUCT;
return UMATCH_NONE;
}
static void
ualea_attach(device_t parent, device_t self, void *aux)
{
struct usbif_attach_arg *uiaa = aux;
struct ualea_softc *sc = device_private(self);
const usb_endpoint_descriptor_t *ed;
char *devinfop;
usbd_status status;
aprint_naive("\n");
aprint_normal("\n");
devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
aprint_normal_dev(self, "%s\n", devinfop);
usbd_devinfo_free(devinfop);
sc->sc_dev = self;
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTSERIAL);
ed = usbd_interface2endpoint_descriptor(uiaa->uiaa_iface, 0);
if (ed == NULL) {
aprint_error_dev(sc->sc_dev, "failed to read endpoint 0\n");
return;
}
if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
UE_GET_XFERTYPE(ed->bmAttributes) != UE_BULK) {
aprint_error_dev(sc->sc_dev, "invalid endpoint\n");
return;
}
sc->sc_maxpktsize = UGETW(ed->wMaxPacketSize);
status = usbd_open_pipe(uiaa->uiaa_iface, ed->bEndpointAddress,
USBD_EXCLUSIVE_USE|USBD_MPSAFE, &sc->sc_pipe);
if (status) {
aprint_error_dev(sc->sc_dev, "failed to open pipe: %d\n",
status);
return;
}
status = usbd_create_xfer(sc->sc_pipe, sc->sc_maxpktsize,
0, 0, &sc->sc_xfer);
if (status) {
aprint_error_dev(sc->sc_dev, "failed to create xfer: %d\n",
status);
return;
}
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(sc->sc_dev, "failed to register power handler"
"\n");
sc->sc_attached = true;
rndsource_setcb(&sc->sc_rnd, ualea_get, sc);
rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
}
static int
ualea_detach(device_t self, int flags)
{
struct ualea_softc *sc = device_private(self);
if (sc->sc_attached)
rnd_detach_source(&sc->sc_rnd);
mutex_enter(&sc->sc_lock);
sc->sc_needed = 0;
mutex_exit(&sc->sc_lock);
if (sc->sc_pipe)
usbd_abort_pipe(sc->sc_pipe);
KASSERT(!sc->sc_inflight);
if (sc->sc_xfer)
usbd_destroy_xfer(sc->sc_xfer);
if (sc->sc_pipe)
usbd_close_pipe(sc->sc_pipe);
mutex_destroy(&sc->sc_lock);
return 0;
}
static void
ualea_xfer(struct ualea_softc *sc)
{
usbd_status status;
KASSERT(mutex_owned(&sc->sc_lock));
KASSERT(sc->sc_attached);
KASSERT(!sc->sc_inflight);
if (sc->sc_needed == 0)
return;
usbd_setup_xfer(sc->sc_xfer, sc, usbd_get_buffer(sc->sc_xfer),
sc->sc_maxpktsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
ualea_xfer_done);
status = usbd_transfer(sc->sc_xfer);
KASSERT(status != USBD_NORMAL_COMPLETION);
if (status != USBD_IN_PROGRESS) {
device_printf(sc->sc_dev, "failed to issue xfer: %s\n",
usbd_errstr(status));
return;
}
sc->sc_inflight = true;
}
static void
ualea_get(size_t nbytes, void *cookie)
{
struct ualea_softc *sc = cookie;
mutex_enter(&sc->sc_lock);
sc->sc_needed = MAX(sc->sc_needed, nbytes);
if (!sc->sc_inflight)
ualea_xfer(sc);
mutex_exit(&sc->sc_lock);
}
static void
ualea_xfer_done(struct usbd_xfer *xfer, void *cookie, usbd_status status)
{
struct ualea_softc *sc = cookie;
void *pkt;
uint32_t pktsize;
if (status) {
device_printf(sc->sc_dev, "xfer failed: %s\n",
usbd_errstr(status));
mutex_enter(&sc->sc_lock);
sc->sc_needed = 0;
sc->sc_inflight = false;
mutex_exit(&sc->sc_lock);
return;
}
usbd_get_xfer_status(xfer, NULL, &pkt, &pktsize, NULL);
KASSERTMSG(pktsize <= sc->sc_maxpktsize,
"pktsize %"PRIu32" > %"PRIu16" (max)",
pktsize, sc->sc_maxpktsize);
mutex_enter(&sc->sc_lock);
rnd_add_data(&sc->sc_rnd, pkt, pktsize, NBBY*pktsize);
sc->sc_needed -= MIN(sc->sc_needed, pktsize);
sc->sc_inflight = false;
ualea_xfer(sc);
mutex_exit(&sc->sc_lock);
}
MODULE(MODULE_CLASS_DRIVER, ualea, NULL);
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
ualea_modcmd(modcmd_t cmd, void *aux)
{
int error = 0;
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
error = config_init_component(cfdriver_ioconf_ualea,
cfattach_ioconf_ualea, cfdata_ioconf_ualea);
#endif
return error;
case MODULE_CMD_FINI:
#ifdef _MODULE
error = config_fini_component(cfdriver_ioconf_ualea,
cfattach_ioconf_ualea, cfdata_ioconf_ualea);
#endif
return error;
default:
return ENOTTY;
}
}