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
32 * $FreeBSD: src/sys/dev/usb/ufm.c,v 1.16 2003/10/04 21:41:01 joe Exp $
33 * $DragonFly: src/sys/dev/usbmisc/ufm/ufm.c,v 1.16 2007/06/28 13:55:12 hasso Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <sys/ioccom.h>
43 #include <sys/fcntl.h>
44 #include <sys/filio.h>
49 #include <sys/select.h>
50 #include <sys/vnode.h>
52 #include <sys/sysctl.h>
53 #include <sys/thread2.h>
55 #include <bus/usb/usb.h>
56 #include <bus/usb/usbdi.h>
57 #include <bus/usb/usbdi_util.h>
59 #include <bus/usb/usbdevs.h>
60 #include <bus/usb/dsbr100io.h>
63 #define DPRINTF(x) if (ufmdebug) kprintf x
64 #define DPRINTFN(n,x) if (ufmdebug>(n)) kprintf x
66 SYSCTL_NODE(_hw_usb, OID_AUTO, ufm, CTLFLAG_RW, 0, "USB ufm");
67 SYSCTL_INT(_hw_usb_ufm, OID_AUTO, debug, CTLFLAG_RW,
68 &ufmdebug, 0, "ufm debug level");
78 #define UFM_CDEV_MAJOR 200
80 static struct dev_ops ufm_ops = {
81 { "ufm", UFM_CDEV_MAJOR, 0 },
88 #define FM_CMD_SET_FREQ 0x01
93 usbd_device_handle sc_udev;
94 usbd_interface_handle sc_iface;
103 #define UFMUNIT(n) (minor(n))
105 USB_DECLARE_DRIVER(ufm);
109 USB_MATCH_START(ufm, uaa);
110 usb_device_descriptor_t *dd;
112 DPRINTFN(10,("ufm_match\n"));
116 dd = usbd_get_device_descriptor(uaa->device);
119 ((UGETW(dd->idVendor) == USB_VENDOR_CYPRESS &&
120 UGETW(dd->idProduct) == USB_PRODUCT_CYPRESS_FMRADIO)))
121 return UMATCH_VENDOR_PRODUCT;
128 USB_ATTACH_START(ufm, sc, uaa);
130 usb_endpoint_descriptor_t *edesc;
131 usbd_device_handle udev;
132 usbd_interface_handle iface;
135 char * ermsg = "<none>";
137 DPRINTFN(10,("ufm_attach: sc=%p\n", sc));
138 usbd_devinfo(uaa->device, 0, devinfo);
140 kprintf("%s: %s\n", device_get_nameunit(sc->sc_dev), devinfo);
142 sc->sc_udev = udev = uaa->device;
144 if ((!uaa->device) || (!uaa->iface)) {
145 ermsg = "device or iface";
148 sc->sc_iface = iface = uaa->iface;
152 r = usbd_endpoint_count(iface, &epcount);
153 if (r != USBD_NORMAL_COMPLETION) {
158 edesc = usbd_interface2endpoint_descriptor(iface, 0);
160 ermsg = "interface endpoint";
163 sc->sc_epaddr = edesc->bEndpointAddress;
165 /* XXX no error trapping, no storing of cdev_t */
166 dev_ops_add(&ufm_ops, -1, device_get_unit(self));
167 make_dev(&ufm_ops, device_get_unit(self),
168 UID_ROOT, GID_OPERATOR,
169 0644, "ufm%d", device_get_unit(self));
171 DPRINTFN(10, ("ufm_attach: %p\n", sc->sc_udev));
173 USB_ATTACH_SUCCESS_RETURN;
176 kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
177 USB_ATTACH_ERROR_RETURN;
182 ufmopen(struct dev_open_args *ap)
184 cdev_t dev = ap->a_head.a_dev;
185 struct ufm_softc *sc;
187 int unit = UFMUNIT(dev);
188 USB_GET_SC_OPEN(ufm, unit, sc);
190 DPRINTFN(5, ("ufmopen: flag=%d, mode=%d, unit=%d\n",
191 ap->a_oflags, ap->a_devtype, unit));
196 if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
204 ufmclose(struct dev_close_args *ap)
206 cdev_t dev = ap->a_head.a_dev;
207 struct ufm_softc *sc;
209 int unit = UFMUNIT(dev);
210 USB_GET_SC(ufm, unit, sc);
212 DPRINTFN(5, ("ufmclose: flag=%d, mode=%d, unit=%d\n",
213 ap->a_fflag, ap->a_devtype, unit));
220 ufm_do_req(struct ufm_softc *sc, u_int8_t reqtype, u_int8_t request,
221 u_int16_t value, u_int16_t index, u_int8_t len, void *retbuf)
223 usb_device_request_t req;
227 req.bmRequestType = reqtype;
228 req.bRequest = request;
229 USETW(req.wValue, value);
230 USETW(req.wIndex, index);
231 USETW(req.wLength, len);
232 err = usbd_do_request_flags(sc->sc_udev, &req, retbuf, 0, NULL,
233 USBD_DEFAULT_TIMEOUT);
236 kprintf("usbd_do_request_flags returned %#x\n", err);
243 ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
245 int freq = *(int *)addr;
249 * Freq now is in Hz. We need to convert it to the frequency
250 * that the radio wants. This frequency is 10.7MHz above
251 * the actual frequency. We then need to convert to
252 * units of 12.5kHz. We add one to the IFM to make rounding
256 freq = (freq + 10700001) / 12500;
257 /* This appears to set the frequency */
258 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
261 /* Not sure what this does */
262 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
269 ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
271 int *valp = (int *)addr;
277 ufm_start(struct ufm_softc *sc, caddr_t addr)
281 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
284 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
293 ufm_stop(struct ufm_softc *sc, caddr_t addr)
297 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
300 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
307 ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
312 * Note, there's a 240ms settle time before the status
313 * will be valid, so tsleep that amount. hz/4 is a good
314 * approximation of that. Since this is a short sleep
315 * we don't try to catch any signals to keep things
318 tsleep(sc, 0, "ufmwait", hz/4);
319 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
328 ufmioctl(struct dev_ioctl_args *ap)
330 cdev_t dev = ap->a_head.a_dev;
331 caddr_t addr = ap->a_data;
332 struct ufm_softc *sc;
334 int unit = UFMUNIT(dev);
337 USB_GET_SC(ufm, unit, sc);
341 error = ufm_set_freq(sc, addr);
344 error = ufm_get_freq(sc, addr);
347 error = ufm_start(sc, addr);
350 error = ufm_stop(sc, addr);
353 error = ufm_get_stat(sc, addr);
363 ufm_detach(device_t self)
365 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
369 DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);