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