2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 M. Warner Losh
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson.
29 * This code includes software developed by the NetBSD Foundation, Inc. and
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #include <sys/stdint.h>
38 #include <sys/stddef.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
56 #include <sys/fcntl.h>
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
62 #define USB_DEBUG_VAR usb_debug
63 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/ufm_ioctl.h>
68 #define UFM_CMD_SET_FREQ 0x01
72 struct usb_fifo_sc sc_fifo;
75 struct usb_device *sc_udev;
85 static device_probe_t ufm_probe;
86 static device_attach_t ufm_attach;
87 static device_detach_t ufm_detach;
89 static usb_fifo_ioctl_t ufm_ioctl;
91 static struct usb_fifo_methods ufm_fifo_methods = {
92 .f_ioctl = &ufm_ioctl,
96 static int ufm_do_req(struct ufm_softc *, uint8_t, uint16_t, uint16_t,
98 static int ufm_set_freq(struct ufm_softc *, void *);
99 static int ufm_get_freq(struct ufm_softc *, void *);
100 static int ufm_start(struct ufm_softc *, void *);
101 static int ufm_stop(struct ufm_softc *, void *);
102 static int ufm_get_stat(struct ufm_softc *, void *);
104 static devclass_t ufm_devclass;
106 static device_method_t ufm_methods[] = {
107 DEVMETHOD(device_probe, ufm_probe),
108 DEVMETHOD(device_attach, ufm_attach),
109 DEVMETHOD(device_detach, ufm_detach),
114 static driver_t ufm_driver = {
116 .methods = ufm_methods,
117 .size = sizeof(struct ufm_softc),
120 static const STRUCT_USB_HOST_ID ufm_devs[] = {
121 {USB_VPI(USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_FMRADIO, 0)},
124 DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, NULL, 0);
125 MODULE_DEPEND(ufm, usb, 1, 1, 1);
126 MODULE_VERSION(ufm, 1);
127 USB_PNP_HOST_INFO(ufm_devs);
130 ufm_probe(device_t dev)
132 struct usb_attach_arg *uaa = device_get_ivars(dev);
134 if (uaa->usb_mode != USB_MODE_HOST)
136 if (uaa->info.bConfigIndex != 0)
138 if (uaa->info.bIfaceIndex != 0)
141 return (usbd_lookup_id_by_uaa(ufm_devs, sizeof(ufm_devs), uaa));
145 ufm_attach(device_t dev)
147 struct usb_attach_arg *uaa = device_get_ivars(dev);
148 struct ufm_softc *sc = device_get_softc(dev);
151 sc->sc_udev = uaa->device;
152 sc->sc_unit = device_get_unit(dev);
154 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
155 device_get_nameunit(dev));
157 mtx_init(&sc->sc_mtx, "ufm lock", NULL, MTX_DEF | MTX_RECURSE);
159 device_set_usb_desc(dev);
161 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
162 &ufm_fifo_methods, &sc->sc_fifo,
163 device_get_unit(dev), -1, uaa->info.bIfaceIndex,
164 UID_ROOT, GID_OPERATOR, 0644);
168 return (0); /* success */
176 ufm_detach(device_t dev)
178 struct ufm_softc *sc = device_get_softc(dev);
180 usb_fifo_detach(&sc->sc_fifo);
182 mtx_destroy(&sc->sc_mtx);
188 ufm_do_req(struct ufm_softc *sc, uint8_t request,
189 uint16_t value, uint16_t index, uint8_t *retbuf)
193 struct usb_device_request req;
196 req.bmRequestType = UT_READ_VENDOR_DEVICE;
197 req.bRequest = request;
198 USETW(req.wValue, value);
199 USETW(req.wIndex, index);
200 USETW(req.wLength, 1);
202 error = usbd_do_request(sc->sc_udev, NULL, &req, buf);
214 ufm_set_freq(struct ufm_softc *sc, void *addr)
216 int freq = *(int *)addr;
219 * Freq now is in Hz. We need to convert it to the frequency
220 * that the radio wants. This frequency is 10.7MHz above
221 * the actual frequency. We then need to convert to
222 * units of 12.5kHz. We add one to the IFM to make rounding
225 mtx_lock(&sc->sc_mtx);
227 mtx_unlock(&sc->sc_mtx);
229 freq = (freq + 10700001) / 12500;
231 /* This appears to set the frequency */
232 if (ufm_do_req(sc, UFM_CMD_SET_FREQ,
233 freq >> 8, freq, NULL) != 0) {
236 /* Not sure what this does */
237 if (ufm_do_req(sc, UFM_CMD0,
238 0x96, 0xb7, NULL) != 0) {
245 ufm_get_freq(struct ufm_softc *sc, void *addr)
247 int *valp = (int *)addr;
249 mtx_lock(&sc->sc_mtx);
251 mtx_unlock(&sc->sc_mtx);
256 ufm_start(struct ufm_softc *sc, void *addr)
260 if (ufm_do_req(sc, UFM_CMD0,
264 if (ufm_do_req(sc, UFM_CMD2,
275 ufm_stop(struct ufm_softc *sc, void *addr)
277 if (ufm_do_req(sc, UFM_CMD0,
281 if (ufm_do_req(sc, UFM_CMD2,
289 ufm_get_stat(struct ufm_softc *sc, void *addr)
294 * Note, there's a 240ms settle time before the status
295 * will be valid, so sleep that amount.
297 usb_pause_mtx(NULL, hz / 4);
299 if (ufm_do_req(sc, UFM_CMD0,
309 ufm_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
312 struct ufm_softc *sc = usb_fifo_softc(fifo);
315 if ((fflags & (FWRITE | FREAD)) != (FWRITE | FREAD)) {
321 error = ufm_set_freq(sc, addr);
324 error = ufm_get_freq(sc, addr);
327 error = ufm_start(sc, addr);
330 error = ufm_stop(sc, addr);
333 error = ufm_get_stat(sc, addr);