1*718cd907Sriastradh /* $NetBSD: ualea.c,v 1.19 2022/03/20 13:18:30 riastradh Exp $ */
24299c628Sriastradh
34299c628Sriastradh /*-
44299c628Sriastradh * Copyright (c) 2017 The NetBSD Foundation, Inc.
54299c628Sriastradh * All rights reserved.
64299c628Sriastradh *
74299c628Sriastradh * This code is derived from software contributed to The NetBSD Foundation
84299c628Sriastradh * by Taylor R. Campbell.
94299c628Sriastradh *
104299c628Sriastradh * Redistribution and use in source and binary forms, with or without
114299c628Sriastradh * modification, are permitted provided that the following conditions
124299c628Sriastradh * are met:
134299c628Sriastradh * 1. Redistributions of source code must retain the above copyright
144299c628Sriastradh * notice, this list of conditions and the following disclaimer.
154299c628Sriastradh * 2. Redistributions in binary form must reproduce the above copyright
164299c628Sriastradh * notice, this list of conditions and the following disclaimer in the
174299c628Sriastradh * documentation and/or other materials provided with the distribution.
184299c628Sriastradh *
194299c628Sriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204299c628Sriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214299c628Sriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224299c628Sriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234299c628Sriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244299c628Sriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254299c628Sriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264299c628Sriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274299c628Sriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284299c628Sriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294299c628Sriastradh * POSSIBILITY OF SUCH DAMAGE.
304299c628Sriastradh */
314299c628Sriastradh
324299c628Sriastradh #include <sys/cdefs.h>
33*718cd907Sriastradh __KERNEL_RCSID(0, "$NetBSD: ualea.c,v 1.19 2022/03/20 13:18:30 riastradh Exp $");
344299c628Sriastradh
354299c628Sriastradh #include <sys/types.h>
364299c628Sriastradh #include <sys/atomic.h>
374299c628Sriastradh #include <sys/device_if.h>
384299c628Sriastradh #include <sys/kmem.h>
39c2488918Sriastradh #include <sys/module.h>
404299c628Sriastradh #include <sys/rndsource.h>
414299c628Sriastradh
424299c628Sriastradh #include <dev/usb/usb.h>
434299c628Sriastradh #include <dev/usb/usbdevs.h>
444299c628Sriastradh #include <dev/usb/usbdi.h>
454299c628Sriastradh #include <dev/usb/usbdi_util.h>
464299c628Sriastradh
474299c628Sriastradh struct ualea_softc {
484299c628Sriastradh device_t sc_dev;
494299c628Sriastradh kmutex_t sc_lock;
504299c628Sriastradh krndsource_t sc_rnd;
514299c628Sriastradh uint16_t sc_maxpktsize;
524299c628Sriastradh struct usbd_pipe *sc_pipe;
53a92ac7e3Sriastradh /*
54a92ac7e3Sriastradh * Lock covers:
55a92ac7e3Sriastradh * - sc_needed
56a92ac7e3Sriastradh * - sc_inflight
57a92ac7e3Sriastradh * - usbd_transfer(sc_xfer)
58a92ac7e3Sriastradh */
594299c628Sriastradh struct usbd_xfer *sc_xfer;
60a92ac7e3Sriastradh size_t sc_needed;
614299c628Sriastradh bool sc_attached:1;
624299c628Sriastradh bool sc_inflight:1;
634299c628Sriastradh };
644299c628Sriastradh
654299c628Sriastradh static int ualea_match(device_t, cfdata_t, void *);
664299c628Sriastradh static void ualea_attach(device_t, device_t, void *);
674299c628Sriastradh static int ualea_detach(device_t, int);
684299c628Sriastradh static void ualea_get(size_t, void *);
694299c628Sriastradh static void ualea_xfer_done(struct usbd_xfer *, void *, usbd_status);
704299c628Sriastradh
714299c628Sriastradh CFATTACH_DECL_NEW(ualea, sizeof(struct ualea_softc),
724299c628Sriastradh ualea_match, ualea_attach, ualea_detach, NULL);
734299c628Sriastradh
744299c628Sriastradh static const struct usb_devno ualea_devs[] = {
75cf753d30Sriastradh { USB_VENDOR_ARANEUS, USB_PRODUCT_ARANEUS_ALEA },
764299c628Sriastradh };
774299c628Sriastradh
784299c628Sriastradh static int
ualea_match(device_t parent,cfdata_t match,void * aux)794299c628Sriastradh ualea_match(device_t parent, cfdata_t match, void *aux)
804299c628Sriastradh {
814299c628Sriastradh struct usbif_attach_arg *uiaa = aux;
824299c628Sriastradh
834299c628Sriastradh if (usb_lookup(ualea_devs, uiaa->uiaa_vendor, uiaa->uiaa_product))
844299c628Sriastradh return UMATCH_VENDOR_PRODUCT;
854299c628Sriastradh
864299c628Sriastradh return UMATCH_NONE;
874299c628Sriastradh }
884299c628Sriastradh
894299c628Sriastradh static void
ualea_attach(device_t parent,device_t self,void * aux)904299c628Sriastradh ualea_attach(device_t parent, device_t self, void *aux)
914299c628Sriastradh {
924299c628Sriastradh struct usbif_attach_arg *uiaa = aux;
934299c628Sriastradh struct ualea_softc *sc = device_private(self);
944299c628Sriastradh const usb_endpoint_descriptor_t *ed;
954299c628Sriastradh char *devinfop;
964299c628Sriastradh usbd_status status;
974299c628Sriastradh
98a92ac7e3Sriastradh /* Print the device info. */
994299c628Sriastradh aprint_naive("\n");
1004299c628Sriastradh aprint_normal("\n");
101a92ac7e3Sriastradh devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
1024299c628Sriastradh aprint_normal_dev(self, "%s\n", devinfop);
1034299c628Sriastradh usbd_devinfo_free(devinfop);
1044299c628Sriastradh
105a92ac7e3Sriastradh /* Initialize the softc. */
1064299c628Sriastradh sc->sc_dev = self;
107698c2023Sriastradh mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTSERIAL);
1084299c628Sriastradh
109a92ac7e3Sriastradh /* Get endpoint descriptor 0. Make sure it's bulk-in. */
110a92ac7e3Sriastradh ed = usbd_interface2endpoint_descriptor(uiaa->uiaa_iface, 0);
1114299c628Sriastradh if (ed == NULL) {
1124299c628Sriastradh aprint_error_dev(sc->sc_dev, "failed to read endpoint 0\n");
1134299c628Sriastradh return;
1144299c628Sriastradh }
1154299c628Sriastradh if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
1164299c628Sriastradh UE_GET_XFERTYPE(ed->bmAttributes) != UE_BULK) {
1174299c628Sriastradh aprint_error_dev(sc->sc_dev, "invalid endpoint\n");
1184299c628Sriastradh return;
1194299c628Sriastradh }
1204299c628Sriastradh
121a92ac7e3Sriastradh /* Remember the maximum packet size. */
1224299c628Sriastradh sc->sc_maxpktsize = UGETW(ed->wMaxPacketSize);
1234299c628Sriastradh
124a92ac7e3Sriastradh /* Open an exclusive MP-safe pipe for endpoint 0. */
125a92ac7e3Sriastradh status = usbd_open_pipe(uiaa->uiaa_iface, ed->bEndpointAddress,
1264299c628Sriastradh USBD_EXCLUSIVE_USE|USBD_MPSAFE, &sc->sc_pipe);
1274299c628Sriastradh if (status) {
1284299c628Sriastradh aprint_error_dev(sc->sc_dev, "failed to open pipe: %d\n",
1294299c628Sriastradh status);
1304299c628Sriastradh return;
1314299c628Sriastradh }
1324299c628Sriastradh
133b8421611Sskrll /* Create an xfer of maximum packet size on the pipe. */
1344299c628Sriastradh status = usbd_create_xfer(sc->sc_pipe, sc->sc_maxpktsize,
135b8421611Sskrll 0, 0, &sc->sc_xfer);
1364299c628Sriastradh if (status) {
1374299c628Sriastradh aprint_error_dev(sc->sc_dev, "failed to create xfer: %d\n",
1384299c628Sriastradh status);
1394299c628Sriastradh return;
1404299c628Sriastradh }
1414299c628Sriastradh
142e8ee63f9Sriastradh if (!pmf_device_register(self, NULL, NULL))
143e8ee63f9Sriastradh aprint_error_dev(sc->sc_dev, "failed to register power handler"
144e8ee63f9Sriastradh "\n");
145e8ee63f9Sriastradh
1464299c628Sriastradh /* Success! We are ready to run. */
1474299c628Sriastradh sc->sc_attached = true;
1484df5e81cSriastradh rndsource_setcb(&sc->sc_rnd, ualea_get, sc);
1494df5e81cSriastradh rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
1504df5e81cSriastradh RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
1514299c628Sriastradh }
1524299c628Sriastradh
1534299c628Sriastradh static int
ualea_detach(device_t self,int flags)1544299c628Sriastradh ualea_detach(device_t self, int flags)
1554299c628Sriastradh {
1564299c628Sriastradh struct ualea_softc *sc = device_private(self);
1574299c628Sriastradh
1584299c628Sriastradh /* Prevent new use of xfer. */
1594df5e81cSriastradh if (sc->sc_attached)
1604df5e81cSriastradh rnd_detach_source(&sc->sc_rnd);
1614299c628Sriastradh
162149a055eSriastradh /* Prevent xfer from rescheduling itself, if still pending. */
163149a055eSriastradh mutex_enter(&sc->sc_lock);
164149a055eSriastradh sc->sc_needed = 0;
165149a055eSriastradh mutex_exit(&sc->sc_lock);
166149a055eSriastradh
1674299c628Sriastradh /* Cancel pending xfer. */
1684299c628Sriastradh if (sc->sc_pipe)
169065aa2e9Sriastradh usbd_abort_pipe(sc->sc_pipe);
1704299c628Sriastradh KASSERT(!sc->sc_inflight);
1714299c628Sriastradh
1724299c628Sriastradh /* All users have drained. Tear it all down. */
1734299c628Sriastradh if (sc->sc_xfer)
1744299c628Sriastradh usbd_destroy_xfer(sc->sc_xfer);
1754299c628Sriastradh if (sc->sc_pipe)
17640e1019eSriastradh usbd_close_pipe(sc->sc_pipe);
1774299c628Sriastradh mutex_destroy(&sc->sc_lock);
1784299c628Sriastradh
1794299c628Sriastradh return 0;
1804299c628Sriastradh }
1814299c628Sriastradh
1824299c628Sriastradh static void
ualea_xfer(struct ualea_softc * sc)183a92ac7e3Sriastradh ualea_xfer(struct ualea_softc *sc)
1844299c628Sriastradh {
1854299c628Sriastradh usbd_status status;
1864299c628Sriastradh
187a92ac7e3Sriastradh KASSERT(mutex_owned(&sc->sc_lock));
188a92ac7e3Sriastradh KASSERT(sc->sc_attached);
189a92ac7e3Sriastradh KASSERT(!sc->sc_inflight);
190a92ac7e3Sriastradh
191a92ac7e3Sriastradh /* Do nothing if we need nothing. */
192a92ac7e3Sriastradh if (sc->sc_needed == 0)
193a92ac7e3Sriastradh return;
194a92ac7e3Sriastradh
19595c71ed7Ssimonb /* Setup the xfer to call ualea_xfer_done with sc. */
19695c71ed7Ssimonb usbd_setup_xfer(sc->sc_xfer, sc, usbd_get_buffer(sc->sc_xfer),
19795c71ed7Ssimonb sc->sc_maxpktsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
19895c71ed7Ssimonb ualea_xfer_done);
19995c71ed7Ssimonb
200a92ac7e3Sriastradh /* Issue xfer or complain if we can't. */
2014299c628Sriastradh status = usbd_transfer(sc->sc_xfer);
2029d6555baSriastradh KASSERT(status != USBD_NORMAL_COMPLETION); /* asynchronous xfer */
2039d6555baSriastradh if (status != USBD_IN_PROGRESS) {
204149a055eSriastradh device_printf(sc->sc_dev, "failed to issue xfer: %s\n",
205149a055eSriastradh usbd_errstr(status));
2064299c628Sriastradh /* We failed -- let someone else have a go. */
207a92ac7e3Sriastradh return;
2084299c628Sriastradh }
209a92ac7e3Sriastradh
210a92ac7e3Sriastradh /* Mark xfer in-flight. */
211a92ac7e3Sriastradh sc->sc_inflight = true;
212a92ac7e3Sriastradh }
213a92ac7e3Sriastradh
214a92ac7e3Sriastradh static void
ualea_get(size_t nbytes,void * cookie)215a92ac7e3Sriastradh ualea_get(size_t nbytes, void *cookie)
216a92ac7e3Sriastradh {
217a92ac7e3Sriastradh struct ualea_softc *sc = cookie;
218a92ac7e3Sriastradh
219a92ac7e3Sriastradh mutex_enter(&sc->sc_lock);
220a92ac7e3Sriastradh sc->sc_needed = MAX(sc->sc_needed, nbytes);
2214df5e81cSriastradh if (!sc->sc_inflight)
222a92ac7e3Sriastradh ualea_xfer(sc);
2234df5e81cSriastradh mutex_exit(&sc->sc_lock);
2244299c628Sriastradh }
2254299c628Sriastradh
2264299c628Sriastradh static void
ualea_xfer_done(struct usbd_xfer * xfer,void * cookie,usbd_status status)2274299c628Sriastradh ualea_xfer_done(struct usbd_xfer *xfer, void *cookie, usbd_status status)
2284299c628Sriastradh {
2294299c628Sriastradh struct ualea_softc *sc = cookie;
2304299c628Sriastradh void *pkt;
2314299c628Sriastradh uint32_t pktsize;
2324299c628Sriastradh
233961b7b01Sriastradh /*
234961b7b01Sriastradh * If the transfer failed, give up -- forget what we need and
235961b7b01Sriastradh * don't reschedule ourselves.
236961b7b01Sriastradh */
2374299c628Sriastradh if (status) {
238149a055eSriastradh device_printf(sc->sc_dev, "xfer failed: %s\n",
239149a055eSriastradh usbd_errstr(status));
240961b7b01Sriastradh mutex_enter(&sc->sc_lock);
241961b7b01Sriastradh sc->sc_needed = 0;
242961b7b01Sriastradh sc->sc_inflight = false;
243961b7b01Sriastradh mutex_exit(&sc->sc_lock);
244961b7b01Sriastradh return;
2454299c628Sriastradh }
2464299c628Sriastradh
247961b7b01Sriastradh /* Get the transferred size. */
2484299c628Sriastradh usbd_get_xfer_status(xfer, NULL, &pkt, &pktsize, NULL);
249961b7b01Sriastradh KASSERTMSG(pktsize <= sc->sc_maxpktsize,
250961b7b01Sriastradh "pktsize %"PRIu32" > %"PRIu16" (max)",
2514299c628Sriastradh pktsize, sc->sc_maxpktsize);
2524299c628Sriastradh
253961b7b01Sriastradh /*
254*718cd907Sriastradh * Enter the data, debit what we contributed from what we need,
255*718cd907Sriastradh * mark the xfer as done, and reschedule the xfer if we still
256*718cd907Sriastradh * need more.
257*718cd907Sriastradh *
258*718cd907Sriastradh * Must enter the data under the lock so it happens atomically
259*718cd907Sriastradh * with updating sc_needed -- otherwise we might hang needing
260*718cd907Sriastradh * entropy and not scheduling xfer. Must not touch pkt after
261*718cd907Sriastradh * clearing sc_inflight and possibly rescheduling the xfer.
262961b7b01Sriastradh */
2639388cbcdSriastradh mutex_enter(&sc->sc_lock);
264*718cd907Sriastradh rnd_add_data(&sc->sc_rnd, pkt, pktsize, NBBY*pktsize);
265a92ac7e3Sriastradh sc->sc_needed -= MIN(sc->sc_needed, pktsize);
2664299c628Sriastradh sc->sc_inflight = false;
267a92ac7e3Sriastradh ualea_xfer(sc);
2689388cbcdSriastradh mutex_exit(&sc->sc_lock);
2694299c628Sriastradh }
270c2488918Sriastradh
271c2488918Sriastradh MODULE(MODULE_CLASS_DRIVER, ualea, NULL);
272c2488918Sriastradh
273c2488918Sriastradh #ifdef _MODULE
274c2488918Sriastradh #include "ioconf.c"
275c2488918Sriastradh #endif
276c2488918Sriastradh
277c2488918Sriastradh static int
ualea_modcmd(modcmd_t cmd,void * aux)278c2488918Sriastradh ualea_modcmd(modcmd_t cmd, void *aux)
279c2488918Sriastradh {
280c2488918Sriastradh int error = 0;
281c2488918Sriastradh
282c2488918Sriastradh switch (cmd) {
283c2488918Sriastradh case MODULE_CMD_INIT:
284c2488918Sriastradh #ifdef _MODULE
285c2488918Sriastradh error = config_init_component(cfdriver_ioconf_ualea,
286c2488918Sriastradh cfattach_ioconf_ualea, cfdata_ioconf_ualea);
287c2488918Sriastradh #endif
288c2488918Sriastradh return error;
289c2488918Sriastradh case MODULE_CMD_FINI:
290c2488918Sriastradh #ifdef _MODULE
291c2488918Sriastradh error = config_fini_component(cfdriver_ioconf_ualea,
292c2488918Sriastradh cfattach_ioconf_ualea, cfdata_ioconf_ualea);
293c2488918Sriastradh #endif
294c2488918Sriastradh return error;
295c2488918Sriastradh default:
296c2488918Sriastradh return ENOTTY;
297c2488918Sriastradh }
298c2488918Sriastradh }
299