2 * Copyright (c) 2001 M. Warner Losh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson.
27 * This code includes software developed by the NetBSD Foundation, Inc. and
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <dev/usb/usb.h>
37 #include <dev/usb/usb_mfunc.h>
38 #include <dev/usb/usb_error.h>
39 #include <dev/usb/ufm_ioctl.h>
41 #define USB_DEBUG_VAR usb2_debug
43 #include <dev/usb/usb_core.h>
44 #include <dev/usb/usb_debug.h>
45 #include <dev/usb/usb_process.h>
46 #include <dev/usb/usb_request.h>
47 #include <dev/usb/usb_lookup.h>
48 #include <dev/usb/usb_util.h>
49 #include <dev/usb/usb_busdma.h>
50 #include <dev/usb/usb_mbuf.h>
51 #include <dev/usb/usb_dev.h>
54 #define UFM_CMD_SET_FREQ 0x01
58 struct usb2_fifo_sc sc_fifo;
61 struct usb2_device *sc_udev;
71 static device_probe_t ufm_probe;
72 static device_attach_t ufm_attach;
73 static device_detach_t ufm_detach;
75 static usb2_fifo_ioctl_t ufm_ioctl;
76 static usb2_fifo_open_t ufm_open;
78 static struct usb2_fifo_methods ufm_fifo_methods = {
79 .f_ioctl = &ufm_ioctl,
84 static int ufm_do_req(struct ufm_softc *, uint8_t, uint16_t, uint16_t,
86 static int ufm_set_freq(struct ufm_softc *, void *);
87 static int ufm_get_freq(struct ufm_softc *, void *);
88 static int ufm_start(struct ufm_softc *, void *);
89 static int ufm_stop(struct ufm_softc *, void *);
90 static int ufm_get_stat(struct ufm_softc *, void *);
92 static devclass_t ufm_devclass;
94 static device_method_t ufm_methods[] = {
95 DEVMETHOD(device_probe, ufm_probe),
96 DEVMETHOD(device_attach, ufm_attach),
97 DEVMETHOD(device_detach, ufm_detach),
101 static driver_t ufm_driver = {
103 .methods = ufm_methods,
104 .size = sizeof(struct ufm_softc),
107 DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, NULL, 0);
108 MODULE_DEPEND(ufm, usb, 1, 1, 1);
111 ufm_probe(device_t dev)
113 struct usb2_attach_arg *uaa = device_get_ivars(dev);
115 if (uaa->usb2_mode != USB_MODE_HOST) {
118 if ((uaa->info.idVendor == USB_VENDOR_CYPRESS) &&
119 (uaa->info.idProduct == USB_PRODUCT_CYPRESS_FMRADIO)) {
126 ufm_attach(device_t dev)
128 struct usb2_attach_arg *uaa = device_get_ivars(dev);
129 struct ufm_softc *sc = device_get_softc(dev);
132 sc->sc_udev = uaa->device;
133 sc->sc_unit = device_get_unit(dev);
135 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
136 device_get_nameunit(dev));
138 mtx_init(&sc->sc_mtx, "ufm lock", NULL, MTX_DEF | MTX_RECURSE);
140 device_set_usb2_desc(dev);
142 error = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx,
143 &ufm_fifo_methods, &sc->sc_fifo,
144 device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
145 UID_ROOT, GID_OPERATOR, 0644);
149 return (0); /* success */
157 ufm_detach(device_t dev)
159 struct ufm_softc *sc = device_get_softc(dev);
161 usb2_fifo_detach(&sc->sc_fifo);
163 mtx_destroy(&sc->sc_mtx);
169 ufm_open(struct usb2_fifo *dev, int fflags)
171 if ((fflags & (FWRITE | FREAD)) != (FWRITE | FREAD)) {
178 ufm_do_req(struct ufm_softc *sc, uint8_t request,
179 uint16_t value, uint16_t index, uint8_t *retbuf)
183 struct usb2_device_request req;
186 req.bmRequestType = UT_READ_VENDOR_DEVICE;
187 req.bRequest = request;
188 USETW(req.wValue, value);
189 USETW(req.wIndex, index);
190 USETW(req.wLength, 1);
192 error = usb2_do_request(sc->sc_udev, NULL, &req, buf);
204 ufm_set_freq(struct ufm_softc *sc, void *addr)
206 int freq = *(int *)addr;
209 * Freq now is in Hz. We need to convert it to the frequency
210 * that the radio wants. This frequency is 10.7MHz above
211 * the actual frequency. We then need to convert to
212 * units of 12.5kHz. We add one to the IFM to make rounding
215 mtx_lock(&sc->sc_mtx);
217 mtx_unlock(&sc->sc_mtx);
219 freq = (freq + 10700001) / 12500;
221 /* This appears to set the frequency */
222 if (ufm_do_req(sc, UFM_CMD_SET_FREQ,
223 freq >> 8, freq, NULL) != 0) {
226 /* Not sure what this does */
227 if (ufm_do_req(sc, UFM_CMD0,
228 0x96, 0xb7, NULL) != 0) {
235 ufm_get_freq(struct ufm_softc *sc, void *addr)
237 int *valp = (int *)addr;
239 mtx_lock(&sc->sc_mtx);
241 mtx_unlock(&sc->sc_mtx);
246 ufm_start(struct ufm_softc *sc, void *addr)
250 if (ufm_do_req(sc, UFM_CMD0,
254 if (ufm_do_req(sc, UFM_CMD2,
265 ufm_stop(struct ufm_softc *sc, void *addr)
267 if (ufm_do_req(sc, UFM_CMD0,
271 if (ufm_do_req(sc, UFM_CMD2,
279 ufm_get_stat(struct ufm_softc *sc, void *addr)
284 * Note, there's a 240ms settle time before the status
285 * will be valid, so sleep that amount.
287 usb2_pause_mtx(NULL, hz / 4);
289 if (ufm_do_req(sc, UFM_CMD0,
299 ufm_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr,
302 struct ufm_softc *sc = fifo->priv_sc0;
307 error = ufm_set_freq(sc, addr);
310 error = ufm_get_freq(sc, addr);
313 error = ufm_start(sc, addr);
316 error = ufm_stop(sc, addr);
319 error = ufm_get_stat(sc, addr);