Home | History | Annotate | Line # | Download | only in usb
ualea.c revision 1.1
      1 /*	$NetBSD: ualea.c,v 1.1 2017/04/17 08:59:37 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.1 2017/04/17 08:59:37 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 	/*
     59 	 * attached true->false only under lock
     60 	 * inflight false->true only under lock
     61 	 * issue xfer only under lock, if attached=true and inflight=false
     62 	 */
     63 };
     64 
     65 static int	ualea_match(device_t, cfdata_t, void *);
     66 static void	ualea_attach(device_t, device_t, void *);
     67 static int	ualea_detach(device_t, int);
     68 static void	ualea_get(size_t, void *);
     69 static void	ualea_xfer_done(struct usbd_xfer *, void *, usbd_status);
     70 
     71 CFATTACH_DECL_NEW(ualea, sizeof(struct ualea_softc),
     72     ualea_match, ualea_attach, ualea_detach, NULL);
     73 
     74 static const struct usb_devno ualea_devs[] = {
     75 	{ USB_VENDOR_ARANEUS,	USB_PRODUCT_ARANEUS_ALEA_II },
     76 };
     77 
     78 static int
     79 ualea_match(device_t parent, cfdata_t match, void *aux)
     80 {
     81 	struct usbif_attach_arg *uiaa = aux;
     82 
     83 	if (usb_lookup(ualea_devs, uiaa->uiaa_vendor, uiaa->uiaa_product))
     84 		return UMATCH_VENDOR_PRODUCT;
     85 
     86 	return UMATCH_NONE;
     87 }
     88 
     89 static void
     90 ualea_attach(device_t parent, device_t self, void *aux)
     91 {
     92 	struct usbif_attach_arg *uiaa = aux;
     93 	struct ualea_softc *sc = device_private(self);
     94 	struct usbd_device *udev = uiaa->uiaa_device;
     95 	struct usbd_interface *uif = uiaa->uiaa_iface;
     96 	const usb_endpoint_descriptor_t *ed;
     97 	char *devinfop;
     98 	usbd_status status;
     99 
    100 	aprint_naive("\n");
    101 	aprint_normal("\n");
    102 
    103 	devinfop = usbd_devinfo_alloc(udev, 0);
    104 	aprint_normal_dev(self, "%s\n", devinfop);
    105 	usbd_devinfo_free(devinfop);
    106 
    107 	sc->sc_dev = self;
    108 	sc->sc_udev = udev;
    109 	sc->sc_uif = uif;
    110 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_USB);
    111 	rndsource_setcb(&sc->sc_rnd, ualea_get, sc);
    112 	rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
    113 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
    114 
    115 	ed = usbd_interface2endpoint_descriptor(uif, 0);
    116 	if (ed == NULL) {
    117 		aprint_error_dev(sc->sc_dev, "failed to read endpoint 0\n");
    118 		return;
    119 	}
    120 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    121 	    UE_GET_XFERTYPE(ed->bmAttributes) != UE_BULK) {
    122 		aprint_error_dev(sc->sc_dev, "invalid endpoint\n");
    123 		return;
    124 	}
    125 
    126 	sc->sc_maxpktsize = UGETW(ed->wMaxPacketSize);
    127 
    128 	status = usbd_open_pipe(uif, ed->bEndpointAddress,
    129 	    USBD_EXCLUSIVE_USE|USBD_MPSAFE, &sc->sc_pipe);
    130 	if (status) {
    131 		aprint_error_dev(sc->sc_dev, "failed to open pipe: %d\n",
    132 		    status);
    133 		return;
    134 	}
    135 
    136 	status = usbd_create_xfer(sc->sc_pipe, sc->sc_maxpktsize,
    137 	    USBD_SHORT_XFER_OK, 0, &sc->sc_xfer);
    138 	if (status) {
    139 		aprint_error_dev(sc->sc_dev, "failed to create xfer: %d\n",
    140 		    status);
    141 		return;
    142 	}
    143 
    144 	usbd_setup_xfer(sc->sc_xfer, sc, usbd_get_buffer(sc->sc_xfer),
    145 	    sc->sc_maxpktsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
    146 	    ualea_xfer_done);
    147 
    148 	/* Success!  We are ready to run.  */
    149 	sc->sc_attached = true;
    150 
    151 	/* Get some initial entropy now.  */
    152 	ualea_get(RND_POOLBITS/NBBY, sc);
    153 }
    154 
    155 static int
    156 ualea_detach(device_t self, int flags)
    157 {
    158 	struct ualea_softc *sc = device_private(self);
    159 
    160 	/* Prevent new use of xfer.  */
    161 	mutex_enter(&sc->sc_lock);
    162 	sc->sc_attached = false;
    163 	mutex_exit(&sc->sc_lock);
    164 
    165 	/* Cancel pending xfer.  */
    166 	if (sc->sc_pipe)
    167 		(void)usbd_abort_pipe(sc->sc_pipe);
    168 	KASSERT(!sc->sc_inflight);
    169 
    170 	/* All users have drained.  Tear it all down.  */
    171 	if (sc->sc_xfer)
    172 		usbd_destroy_xfer(sc->sc_xfer);
    173 	if (sc->sc_pipe)
    174 		(void)usbd_close_pipe(sc->sc_pipe);
    175 	rnd_detach_source(&sc->sc_rnd);
    176 	mutex_destroy(&sc->sc_lock);
    177 
    178 	return 0;
    179 }
    180 
    181 static void
    182 ualea_get(size_t nbytes, void *cookie)
    183 {
    184 	struct ualea_softc *sc = cookie;
    185 	usbd_status status;
    186 
    187 	mutex_enter(&sc->sc_lock);
    188 	if (!sc->sc_attached)
    189 		goto out;
    190 	if (sc->sc_inflight)
    191 		goto out;
    192 	sc->sc_inflight = true;
    193 	status = usbd_transfer(sc->sc_xfer);
    194 	if (status && status != USBD_IN_PROGRESS) {
    195 		aprint_error_dev(sc->sc_dev, "failed to issue xfer: %d\n",
    196 		    status);
    197 		/* We failed -- let someone else have a go.  */
    198 		sc->sc_inflight = false;
    199 	}
    200 out:	mutex_exit(&sc->sc_lock);
    201 }
    202 
    203 static void
    204 ualea_xfer_done(struct usbd_xfer *xfer, void *cookie, usbd_status status)
    205 {
    206 	struct ualea_softc *sc = cookie;
    207 	void *pkt;
    208 	uint32_t pktsize;
    209 
    210 	/* Check the transfer status.  */
    211 	if (status) {
    212 		aprint_error_dev(sc->sc_dev, "xfer failed: %d\n", status);
    213 		goto out;
    214 	}
    215 
    216 	/* Get and sanity-check the transferred size.  */
    217 	usbd_get_xfer_status(xfer, NULL, &pkt, &pktsize, NULL);
    218 	if (pktsize > sc->sc_maxpktsize) {
    219 		aprint_error_dev(sc->sc_dev,
    220 		    "bogus packet size: %"PRIu32" > %"PRIu16" (max), ignoring"
    221 		    "\n",
    222 		    pktsize, sc->sc_maxpktsize);
    223 		goto out;
    224 	}
    225 
    226 	/* Add the data to the pool.  */
    227 	rnd_add_data(&sc->sc_rnd, pkt, pktsize, NBBY*pktsize);
    228 
    229 out:
    230 	/* Allow subsequent transfers.  */
    231 	sc->sc_inflight = false;
    232 }
    233