Home | History | Annotate | Line # | Download | only in usb
ualea.c revision 1.3
      1 /*	$NetBSD: ualea.c,v 1.3 2017/04/17 15:43:40 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ualea.c,v 1.3 2017/04/17 15:43:40 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/atomic.h>
     37 #include <sys/device_if.h>
     38 #include <sys/kmem.h>
     39 #include <sys/rndpool.h>
     40 #include <sys/rndsource.h>
     41 
     42 #include <dev/usb/usb.h>
     43 #include <dev/usb/usbdevs.h>
     44 #include <dev/usb/usbdi.h>
     45 #include <dev/usb/usbdi_util.h>
     46 
     47 struct ualea_softc {
     48 	device_t		sc_dev;
     49 	struct usbd_device	*sc_udev;
     50 	struct usbd_interface	*sc_uif;
     51 	kmutex_t		sc_lock;
     52 	krndsource_t		sc_rnd;
     53 	uint16_t		sc_maxpktsize;
     54 	struct usbd_pipe	*sc_pipe;
     55 	struct usbd_xfer	*sc_xfer;
     56 	bool			sc_attached:1;
     57 	bool			sc_inflight:1;
     58 	/* lock covers sc_attached, sc_inflight, and usbd_transfer(sc_xfer) */
     59 };
     60 
     61 static int	ualea_match(device_t, cfdata_t, void *);
     62 static void	ualea_attach(device_t, device_t, void *);
     63 static int	ualea_detach(device_t, int);
     64 static void	ualea_get(size_t, void *);
     65 static void	ualea_xfer_done(struct usbd_xfer *, void *, usbd_status);
     66 
     67 CFATTACH_DECL_NEW(ualea, sizeof(struct ualea_softc),
     68     ualea_match, ualea_attach, ualea_detach, NULL);
     69 
     70 static const struct usb_devno ualea_devs[] = {
     71 	{ USB_VENDOR_ARANEUS,	USB_PRODUCT_ARANEUS_ALEA_II },
     72 };
     73 
     74 static int
     75 ualea_match(device_t parent, cfdata_t match, void *aux)
     76 {
     77 	struct usbif_attach_arg *uiaa = aux;
     78 
     79 	if (usb_lookup(ualea_devs, uiaa->uiaa_vendor, uiaa->uiaa_product))
     80 		return UMATCH_VENDOR_PRODUCT;
     81 
     82 	return UMATCH_NONE;
     83 }
     84 
     85 static void
     86 ualea_attach(device_t parent, device_t self, void *aux)
     87 {
     88 	struct usbif_attach_arg *uiaa = aux;
     89 	struct ualea_softc *sc = device_private(self);
     90 	struct usbd_device *udev = uiaa->uiaa_device;
     91 	struct usbd_interface *uif = uiaa->uiaa_iface;
     92 	const usb_endpoint_descriptor_t *ed;
     93 	char *devinfop;
     94 	usbd_status status;
     95 
     96 	aprint_naive("\n");
     97 	aprint_normal("\n");
     98 
     99 	devinfop = usbd_devinfo_alloc(udev, 0);
    100 	aprint_normal_dev(self, "%s\n", devinfop);
    101 	usbd_devinfo_free(devinfop);
    102 
    103 	sc->sc_dev = self;
    104 	sc->sc_udev = udev;
    105 	sc->sc_uif = uif;
    106 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_USB);
    107 	rndsource_setcb(&sc->sc_rnd, ualea_get, sc);
    108 	rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
    109 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
    110 
    111 	ed = usbd_interface2endpoint_descriptor(uif, 0);
    112 	if (ed == NULL) {
    113 		aprint_error_dev(sc->sc_dev, "failed to read endpoint 0\n");
    114 		return;
    115 	}
    116 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    117 	    UE_GET_XFERTYPE(ed->bmAttributes) != UE_BULK) {
    118 		aprint_error_dev(sc->sc_dev, "invalid endpoint\n");
    119 		return;
    120 	}
    121 
    122 	sc->sc_maxpktsize = UGETW(ed->wMaxPacketSize);
    123 
    124 	status = usbd_open_pipe(uif, ed->bEndpointAddress,
    125 	    USBD_EXCLUSIVE_USE|USBD_MPSAFE, &sc->sc_pipe);
    126 	if (status) {
    127 		aprint_error_dev(sc->sc_dev, "failed to open pipe: %d\n",
    128 		    status);
    129 		return;
    130 	}
    131 
    132 	status = usbd_create_xfer(sc->sc_pipe, sc->sc_maxpktsize,
    133 	    USBD_SHORT_XFER_OK, 0, &sc->sc_xfer);
    134 	if (status) {
    135 		aprint_error_dev(sc->sc_dev, "failed to create xfer: %d\n",
    136 		    status);
    137 		return;
    138 	}
    139 
    140 	usbd_setup_xfer(sc->sc_xfer, sc, usbd_get_buffer(sc->sc_xfer),
    141 	    sc->sc_maxpktsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
    142 	    ualea_xfer_done);
    143 
    144 	/* Success!  We are ready to run.  */
    145 	mutex_enter(&sc->sc_lock);
    146 	sc->sc_attached = true;
    147 	mutex_exit(&sc->sc_lock);
    148 
    149 	/* Get some initial entropy now.  */
    150 	ualea_get(RND_POOLBITS/NBBY, sc);
    151 }
    152 
    153 static int
    154 ualea_detach(device_t self, int flags)
    155 {
    156 	struct ualea_softc *sc = device_private(self);
    157 
    158 	/* Prevent new use of xfer.  */
    159 	mutex_enter(&sc->sc_lock);
    160 	sc->sc_attached = false;
    161 	mutex_exit(&sc->sc_lock);
    162 
    163 	/* Cancel pending xfer.  */
    164 	if (sc->sc_pipe)
    165 		(void)usbd_abort_pipe(sc->sc_pipe);
    166 	KASSERT(!sc->sc_inflight);
    167 
    168 	/* All users have drained.  Tear it all down.  */
    169 	if (sc->sc_xfer)
    170 		usbd_destroy_xfer(sc->sc_xfer);
    171 	if (sc->sc_pipe)
    172 		(void)usbd_close_pipe(sc->sc_pipe);
    173 	rnd_detach_source(&sc->sc_rnd);
    174 	mutex_destroy(&sc->sc_lock);
    175 
    176 	return 0;
    177 }
    178 
    179 static void
    180 ualea_get(size_t nbytes, void *cookie)
    181 {
    182 	struct ualea_softc *sc = cookie;
    183 	usbd_status status;
    184 
    185 	mutex_enter(&sc->sc_lock);
    186 	if (!sc->sc_attached)
    187 		goto out;
    188 	if (sc->sc_inflight)
    189 		goto out;
    190 	sc->sc_inflight = true;
    191 	status = usbd_transfer(sc->sc_xfer);
    192 	if (status && status != USBD_IN_PROGRESS) {
    193 		aprint_error_dev(sc->sc_dev, "failed to issue xfer: %d\n",
    194 		    status);
    195 		/* We failed -- let someone else have a go.  */
    196 		sc->sc_inflight = false;
    197 	}
    198 out:	mutex_exit(&sc->sc_lock);
    199 }
    200 
    201 static void
    202 ualea_xfer_done(struct usbd_xfer *xfer, void *cookie, usbd_status status)
    203 {
    204 	struct ualea_softc *sc = cookie;
    205 	void *pkt;
    206 	uint32_t pktsize;
    207 
    208 	/* Check the transfer status.  */
    209 	if (status) {
    210 		aprint_error_dev(sc->sc_dev, "xfer failed: %d\n", status);
    211 		goto out;
    212 	}
    213 
    214 	/* Get and sanity-check the transferred size.  */
    215 	usbd_get_xfer_status(xfer, NULL, &pkt, &pktsize, NULL);
    216 	if (pktsize > sc->sc_maxpktsize) {
    217 		aprint_error_dev(sc->sc_dev,
    218 		    "bogus packet size: %"PRIu32" > %"PRIu16" (max), ignoring"
    219 		    "\n",
    220 		    pktsize, sc->sc_maxpktsize);
    221 		goto out;
    222 	}
    223 
    224 	/* Add the data to the pool.  */
    225 	rnd_add_data(&sc->sc_rnd, pkt, pktsize, NBBY*pktsize);
    226 
    227 out:
    228 	/* Allow subsequent transfers.  */
    229 	mutex_enter(&sc->sc_lock);
    230 	sc->sc_inflight = false;
    231 	mutex_exit(&sc->sc_lock);
    232 }
    233