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