2021-05-29 11:45:19 +03:00
|
|
|
/* $NetBSD: ualea.c,v 1.13 2021/05/29 08:45:19 riastradh Exp $ */
|
2017-04-17 11:59:37 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2017 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Taylor R. Campbell.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2021-05-29 11:45:19 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ualea.c,v 1.13 2021/05/29 08:45:19 riastradh Exp $");
|
2017-04-17 11:59:37 +03:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/atomic.h>
|
|
|
|
#include <sys/device_if.h>
|
|
|
|
#include <sys/kmem.h>
|
2017-04-19 03:01:38 +03:00
|
|
|
#include <sys/module.h>
|
2017-04-17 11:59:37 +03:00
|
|
|
#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;
|
2017-07-15 08:46:09 +03:00
|
|
|
/*
|
|
|
|
* Lock covers:
|
|
|
|
* - sc_needed
|
|
|
|
* - sc_inflight
|
|
|
|
* - usbd_transfer(sc_xfer)
|
|
|
|
*/
|
2017-04-17 11:59:37 +03:00
|
|
|
struct usbd_xfer *sc_xfer;
|
2017-07-15 08:46:09 +03:00
|
|
|
size_t sc_needed;
|
2017-04-17 11:59:37 +03:00
|
|
|
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[] = {
|
2017-04-18 22:09:12 +03:00
|
|
|
{ USB_VENDOR_ARANEUS, USB_PRODUCT_ARANEUS_ALEA },
|
2017-04-17 11:59:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-07-15 08:46:09 +03:00
|
|
|
/* Print the device info. */
|
2017-04-17 11:59:37 +03:00
|
|
|
aprint_naive("\n");
|
|
|
|
aprint_normal("\n");
|
2017-07-15 08:46:09 +03:00
|
|
|
devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
|
2017-04-17 11:59:37 +03:00
|
|
|
aprint_normal_dev(self, "%s\n", devinfop);
|
|
|
|
usbd_devinfo_free(devinfop);
|
|
|
|
|
2017-07-15 08:46:09 +03:00
|
|
|
/* Initialize the softc. */
|
2017-04-17 11:59:37 +03:00
|
|
|
sc->sc_dev = self;
|
2017-04-17 19:42:07 +03:00
|
|
|
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
|
2017-04-17 11:59:37 +03:00
|
|
|
|
2017-07-15 08:46:09 +03:00
|
|
|
/* Get endpoint descriptor 0. Make sure it's bulk-in. */
|
|
|
|
ed = usbd_interface2endpoint_descriptor(uiaa->uiaa_iface, 0);
|
2017-04-17 11:59:37 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-15 08:46:09 +03:00
|
|
|
/* Remember the maximum packet size. */
|
2017-04-17 11:59:37 +03:00
|
|
|
sc->sc_maxpktsize = UGETW(ed->wMaxPacketSize);
|
|
|
|
|
2017-07-15 08:46:09 +03:00
|
|
|
/* Open an exclusive MP-safe pipe for endpoint 0. */
|
|
|
|
status = usbd_open_pipe(uiaa->uiaa_iface, ed->bEndpointAddress,
|
2017-04-17 11:59:37 +03:00
|
|
|
USBD_EXCLUSIVE_USE|USBD_MPSAFE, &sc->sc_pipe);
|
|
|
|
if (status) {
|
|
|
|
aprint_error_dev(sc->sc_dev, "failed to open pipe: %d\n",
|
|
|
|
status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-21 16:57:11 +03:00
|
|
|
/* Create an xfer of maximum packet size on the pipe. */
|
2017-04-17 11:59:37 +03:00
|
|
|
status = usbd_create_xfer(sc->sc_pipe, sc->sc_maxpktsize,
|
2018-01-21 16:57:11 +03:00
|
|
|
0, 0, &sc->sc_xfer);
|
2017-04-17 11:59:37 +03:00
|
|
|
if (status) {
|
|
|
|
aprint_error_dev(sc->sc_dev, "failed to create xfer: %d\n",
|
|
|
|
status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-29 11:45:19 +03:00
|
|
|
if (!pmf_device_register(self, NULL, NULL))
|
|
|
|
aprint_error_dev(sc->sc_dev, "failed to register power handler"
|
|
|
|
"\n");
|
|
|
|
|
2017-04-17 11:59:37 +03:00
|
|
|
/* Success! We are ready to run. */
|
|
|
|
sc->sc_attached = true;
|
2020-04-30 06:40:52 +03:00
|
|
|
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);
|
2017-04-17 11:59:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ualea_detach(device_t self, int flags)
|
|
|
|
{
|
|
|
|
struct ualea_softc *sc = device_private(self);
|
|
|
|
|
|
|
|
/* Prevent new use of xfer. */
|
2020-04-30 06:40:52 +03:00
|
|
|
if (sc->sc_attached)
|
|
|
|
rnd_detach_source(&sc->sc_rnd);
|
2017-04-17 11:59:37 +03:00
|
|
|
|
|
|
|
/* Cancel pending xfer. */
|
|
|
|
if (sc->sc_pipe)
|
|
|
|
(void)usbd_abort_pipe(sc->sc_pipe);
|
|
|
|
KASSERT(!sc->sc_inflight);
|
|
|
|
|
|
|
|
/* All users have drained. Tear it all down. */
|
|
|
|
if (sc->sc_xfer)
|
|
|
|
usbd_destroy_xfer(sc->sc_xfer);
|
|
|
|
if (sc->sc_pipe)
|
|
|
|
(void)usbd_close_pipe(sc->sc_pipe);
|
|
|
|
mutex_destroy(&sc->sc_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-07-15 08:46:09 +03:00
|
|
|
ualea_xfer(struct ualea_softc *sc)
|
2017-04-17 11:59:37 +03:00
|
|
|
{
|
|
|
|
usbd_status status;
|
|
|
|
|
2017-07-15 08:46:09 +03:00
|
|
|
KASSERT(mutex_owned(&sc->sc_lock));
|
|
|
|
KASSERT(sc->sc_attached);
|
|
|
|
KASSERT(!sc->sc_inflight);
|
|
|
|
|
|
|
|
/* Do nothing if we need nothing. */
|
|
|
|
if (sc->sc_needed == 0)
|
|
|
|
return;
|
|
|
|
|
2020-07-13 16:53:04 +03:00
|
|
|
/* Setup the xfer to call ualea_xfer_done with sc. */
|
|
|
|
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);
|
|
|
|
|
2017-07-15 08:46:09 +03:00
|
|
|
/* Issue xfer or complain if we can't. */
|
2017-04-17 11:59:37 +03:00
|
|
|
status = usbd_transfer(sc->sc_xfer);
|
2020-04-30 06:24:28 +03:00
|
|
|
KASSERT(status != USBD_NORMAL_COMPLETION); /* asynchronous xfer */
|
|
|
|
if (status != USBD_IN_PROGRESS) {
|
2017-04-17 11:59:37 +03:00
|
|
|
aprint_error_dev(sc->sc_dev, "failed to issue xfer: %d\n",
|
|
|
|
status);
|
|
|
|
/* We failed -- let someone else have a go. */
|
2017-07-15 08:46:09 +03:00
|
|
|
return;
|
2017-04-17 11:59:37 +03:00
|
|
|
}
|
2017-07-15 08:46:09 +03:00
|
|
|
|
|
|
|
/* Mark xfer in-flight. */
|
|
|
|
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);
|
2020-04-30 06:40:52 +03:00
|
|
|
if (!sc->sc_inflight)
|
|
|
|
ualea_xfer(sc);
|
|
|
|
mutex_exit(&sc->sc_lock);
|
2017-04-17 11:59:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ualea_xfer_done(struct usbd_xfer *xfer, void *cookie, usbd_status status)
|
|
|
|
{
|
|
|
|
struct ualea_softc *sc = cookie;
|
|
|
|
void *pkt;
|
|
|
|
uint32_t pktsize;
|
|
|
|
|
|
|
|
/* Check the transfer status. */
|
|
|
|
if (status) {
|
|
|
|
aprint_error_dev(sc->sc_dev, "xfer failed: %d\n", status);
|
2017-07-19 02:11:01 +03:00
|
|
|
return;
|
2017-04-17 11:59:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get and sanity-check the transferred size. */
|
|
|
|
usbd_get_xfer_status(xfer, NULL, &pkt, &pktsize, NULL);
|
|
|
|
if (pktsize > sc->sc_maxpktsize) {
|
|
|
|
aprint_error_dev(sc->sc_dev,
|
|
|
|
"bogus packet size: %"PRIu32" > %"PRIu16" (max), ignoring"
|
|
|
|
"\n",
|
|
|
|
pktsize, sc->sc_maxpktsize);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the data to the pool. */
|
|
|
|
rnd_add_data(&sc->sc_rnd, pkt, pktsize, NBBY*pktsize);
|
|
|
|
|
|
|
|
out:
|
2017-04-17 12:16:13 +03:00
|
|
|
mutex_enter(&sc->sc_lock);
|
2017-07-15 08:46:09 +03:00
|
|
|
|
|
|
|
/* Debit what we contributed from what we need. */
|
|
|
|
sc->sc_needed -= MIN(sc->sc_needed, pktsize);
|
|
|
|
|
|
|
|
/* Mark xfer done. */
|
2017-04-17 11:59:37 +03:00
|
|
|
sc->sc_inflight = false;
|
2017-07-15 08:46:09 +03:00
|
|
|
|
|
|
|
/* Reissue xfer if we still need more. */
|
|
|
|
ualea_xfer(sc);
|
|
|
|
|
2017-04-17 12:16:13 +03:00
|
|
|
mutex_exit(&sc->sc_lock);
|
2017-04-17 11:59:37 +03:00
|
|
|
}
|
2017-04-19 03:01:38 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|