ualea.c revision 1.6 1 /* $NetBSD: ualea.c,v 1.6 2017/04/19 00:01:38 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.6 2017/04/19 00:01:38 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/module.h>
40 #include <sys/rndpool.h>
41 #include <sys/rndsource.h>
42
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdevs.h>
45 #include <dev/usb/usbdi.h>
46 #include <dev/usb/usbdi_util.h>
47
48 struct ualea_softc {
49 device_t sc_dev;
50 struct usbd_device *sc_udev;
51 struct usbd_interface *sc_uif;
52 kmutex_t sc_lock;
53 krndsource_t sc_rnd;
54 uint16_t sc_maxpktsize;
55 struct usbd_pipe *sc_pipe;
56 struct usbd_xfer *sc_xfer;
57 bool sc_attached:1;
58 bool sc_inflight:1;
59 /* lock covers sc_attached, sc_inflight, and usbd_transfer(sc_xfer) */
60 };
61
62 static int ualea_match(device_t, cfdata_t, void *);
63 static void ualea_attach(device_t, device_t, void *);
64 static int ualea_detach(device_t, int);
65 static void ualea_get(size_t, void *);
66 static void ualea_xfer_done(struct usbd_xfer *, void *, usbd_status);
67
68 CFATTACH_DECL_NEW(ualea, sizeof(struct ualea_softc),
69 ualea_match, ualea_attach, ualea_detach, NULL);
70
71 static const struct usb_devno ualea_devs[] = {
72 { USB_VENDOR_ARANEUS, USB_PRODUCT_ARANEUS_ALEA },
73 };
74
75 static int
76 ualea_match(device_t parent, cfdata_t match, void *aux)
77 {
78 struct usbif_attach_arg *uiaa = aux;
79
80 if (usb_lookup(ualea_devs, uiaa->uiaa_vendor, uiaa->uiaa_product))
81 return UMATCH_VENDOR_PRODUCT;
82
83 return UMATCH_NONE;
84 }
85
86 static void
87 ualea_attach(device_t parent, device_t self, void *aux)
88 {
89 struct usbif_attach_arg *uiaa = aux;
90 struct ualea_softc *sc = device_private(self);
91 struct usbd_device *udev = uiaa->uiaa_device;
92 struct usbd_interface *uif = uiaa->uiaa_iface;
93 const usb_endpoint_descriptor_t *ed;
94 char *devinfop;
95 usbd_status status;
96
97 aprint_naive("\n");
98 aprint_normal("\n");
99
100 devinfop = usbd_devinfo_alloc(udev, 0);
101 aprint_normal_dev(self, "%s\n", devinfop);
102 usbd_devinfo_free(devinfop);
103
104 sc->sc_dev = self;
105 sc->sc_udev = udev;
106 sc->sc_uif = uif;
107 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
108 rndsource_setcb(&sc->sc_rnd, ualea_get, sc);
109 rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG,
110 RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
111
112 ed = usbd_interface2endpoint_descriptor(uif, 0);
113 if (ed == NULL) {
114 aprint_error_dev(sc->sc_dev, "failed to read endpoint 0\n");
115 return;
116 }
117 if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
118 UE_GET_XFERTYPE(ed->bmAttributes) != UE_BULK) {
119 aprint_error_dev(sc->sc_dev, "invalid endpoint\n");
120 return;
121 }
122
123 sc->sc_maxpktsize = UGETW(ed->wMaxPacketSize);
124
125 status = usbd_open_pipe(uif, ed->bEndpointAddress,
126 USBD_EXCLUSIVE_USE|USBD_MPSAFE, &sc->sc_pipe);
127 if (status) {
128 aprint_error_dev(sc->sc_dev, "failed to open pipe: %d\n",
129 status);
130 return;
131 }
132
133 status = usbd_create_xfer(sc->sc_pipe, sc->sc_maxpktsize,
134 USBD_SHORT_XFER_OK, 0, &sc->sc_xfer);
135 if (status) {
136 aprint_error_dev(sc->sc_dev, "failed to create xfer: %d\n",
137 status);
138 return;
139 }
140
141 usbd_setup_xfer(sc->sc_xfer, sc, usbd_get_buffer(sc->sc_xfer),
142 sc->sc_maxpktsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
143 ualea_xfer_done);
144
145 /* Success! We are ready to run. */
146 mutex_enter(&sc->sc_lock);
147 sc->sc_attached = true;
148 mutex_exit(&sc->sc_lock);
149
150 /* Get some initial entropy now. */
151 ualea_get(RND_POOLBITS/NBBY, sc);
152 }
153
154 static int
155 ualea_detach(device_t self, int flags)
156 {
157 struct ualea_softc *sc = device_private(self);
158
159 /* Prevent new use of xfer. */
160 mutex_enter(&sc->sc_lock);
161 sc->sc_attached = false;
162 mutex_exit(&sc->sc_lock);
163
164 /* Cancel pending xfer. */
165 if (sc->sc_pipe)
166 (void)usbd_abort_pipe(sc->sc_pipe);
167 KASSERT(!sc->sc_inflight);
168
169 /* All users have drained. Tear it all down. */
170 if (sc->sc_xfer)
171 usbd_destroy_xfer(sc->sc_xfer);
172 if (sc->sc_pipe)
173 (void)usbd_close_pipe(sc->sc_pipe);
174 rnd_detach_source(&sc->sc_rnd);
175 mutex_destroy(&sc->sc_lock);
176
177 return 0;
178 }
179
180 static void
181 ualea_get(size_t nbytes, void *cookie)
182 {
183 struct ualea_softc *sc = cookie;
184 usbd_status status;
185
186 mutex_enter(&sc->sc_lock);
187 if (!sc->sc_attached)
188 goto out;
189 if (sc->sc_inflight)
190 goto out;
191 sc->sc_inflight = true;
192 status = usbd_transfer(sc->sc_xfer);
193 if (status && status != USBD_IN_PROGRESS) {
194 aprint_error_dev(sc->sc_dev, "failed to issue xfer: %d\n",
195 status);
196 /* We failed -- let someone else have a go. */
197 sc->sc_inflight = false;
198 }
199 out: mutex_exit(&sc->sc_lock);
200 }
201
202 static void
203 ualea_xfer_done(struct usbd_xfer *xfer, void *cookie, usbd_status status)
204 {
205 struct ualea_softc *sc = cookie;
206 void *pkt;
207 uint32_t pktsize;
208
209 /* Check the transfer status. */
210 if (status) {
211 aprint_error_dev(sc->sc_dev, "xfer failed: %d\n", status);
212 goto out;
213 }
214
215 /* Get and sanity-check the transferred size. */
216 usbd_get_xfer_status(xfer, NULL, &pkt, &pktsize, NULL);
217 if (pktsize > sc->sc_maxpktsize) {
218 aprint_error_dev(sc->sc_dev,
219 "bogus packet size: %"PRIu32" > %"PRIu16" (max), ignoring"
220 "\n",
221 pktsize, sc->sc_maxpktsize);
222 goto out;
223 }
224
225 /* Add the data to the pool. */
226 rnd_add_data(&sc->sc_rnd, pkt, pktsize, NBBY*pktsize);
227
228 out:
229 /* Allow subsequent transfers. */
230 mutex_enter(&sc->sc_lock);
231 sc->sc_inflight = false;
232 mutex_exit(&sc->sc_lock);
233 }
234
235 MODULE(MODULE_CLASS_DRIVER, ualea, NULL);
236
237 #ifdef _MODULE
238 #include "ioconf.c"
239 #endif
240
241 static int
242 ualea_modcmd(modcmd_t cmd, void *aux)
243 {
244 int error = 0;
245
246 switch (cmd) {
247 case MODULE_CMD_INIT:
248 #ifdef _MODULE
249 error = config_init_component(cfdriver_ioconf_ualea,
250 cfattach_ioconf_ualea, cfdata_ioconf_ualea);
251 #endif
252 return error;
253 case MODULE_CMD_FINI:
254 #ifdef _MODULE
255 error = config_fini_component(cfdriver_ioconf_ualea,
256 cfattach_ioconf_ualea, cfdata_ioconf_ualea);
257 #endif
258 return error;
259 default:
260 return ENOTTY;
261 }
262 }
263