1/*- 2 * Copyright (c) 2001 M. Warner Losh 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 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 28 * its contributors. 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD$"); 33 34 35#include <sys/stdint.h> 36#include <sys/stddef.h> 37#include <sys/param.h> 38#include <sys/queue.h> 39#include <sys/types.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/bus.h> 43#include <sys/module.h> 44#include <sys/lock.h> 45#include <sys/mutex.h> 46#include <sys/condvar.h> 47#include <sys/sysctl.h> 48#include <sys/sx.h> 49#include <sys/unistd.h> 50#include <sys/callout.h> 51#include <sys/malloc.h> 52#include <sys/priv.h> 53#include <sys/conf.h> 54#include <sys/fcntl.h> 55 56#include <dev/usb/usb.h> 57#include <dev/usb/usbdi.h> 58#include "usbdevs.h" 59 60#define USB_DEBUG_VAR usb_debug 61#include <dev/usb/usb_debug.h> 62 63#include <dev/usb/ufm_ioctl.h> 64 65#define UFM_CMD0 0x00 66#define UFM_CMD_SET_FREQ 0x01 67#define UFM_CMD2 0x02 68 69struct ufm_softc { 70 struct usb_fifo_sc sc_fifo; 71 struct mtx sc_mtx; 72 73 struct usb_device *sc_udev; 74 75 uint32_t sc_unit; 76 uint32_t sc_freq; 77 78 uint8_t sc_name[16]; 79}; 80 81/* prototypes */ 82 83static device_probe_t ufm_probe; 84static device_attach_t ufm_attach; 85static device_detach_t ufm_detach; 86 87static usb_fifo_ioctl_t ufm_ioctl; 88 89static struct usb_fifo_methods ufm_fifo_methods = { 90 .f_ioctl = &ufm_ioctl, 91 .basename[0] = "ufm", 92}; 93 94static int ufm_do_req(struct ufm_softc *, uint8_t, uint16_t, uint16_t, 95 uint8_t *); 96static int ufm_set_freq(struct ufm_softc *, void *); 97static int ufm_get_freq(struct ufm_softc *, void *); 98static int ufm_start(struct ufm_softc *, void *); 99static int ufm_stop(struct ufm_softc *, void *); 100static int ufm_get_stat(struct ufm_softc *, void *); 101 102static devclass_t ufm_devclass; 103 104static device_method_t ufm_methods[] = { 105 DEVMETHOD(device_probe, ufm_probe), 106 DEVMETHOD(device_attach, ufm_attach), 107 DEVMETHOD(device_detach, ufm_detach), 108 109 DEVMETHOD_END 110}; 111 112static driver_t ufm_driver = { 113 .name = "ufm", 114 .methods = ufm_methods, 115 .size = sizeof(struct ufm_softc), 116}; 117 118static const STRUCT_USB_HOST_ID ufm_devs[] = { 119 {USB_VPI(USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_FMRADIO, 0)}, 120}; 121 122DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, NULL, 0); 123MODULE_DEPEND(ufm, usb, 1, 1, 1); 124MODULE_VERSION(ufm, 1); 125USB_PNP_HOST_INFO(ufm_devs); 126 127static int 128ufm_probe(device_t dev) 129{ 130 struct usb_attach_arg *uaa = device_get_ivars(dev); 131 132 if (uaa->usb_mode != USB_MODE_HOST) 133 return (ENXIO); 134 if (uaa->info.bConfigIndex != 0) 135 return (ENXIO); 136 if (uaa->info.bIfaceIndex != 0) 137 return (ENXIO); 138 139 return (usbd_lookup_id_by_uaa(ufm_devs, sizeof(ufm_devs), uaa)); 140} 141 142static int 143ufm_attach(device_t dev) 144{ 145 struct usb_attach_arg *uaa = device_get_ivars(dev); 146 struct ufm_softc *sc = device_get_softc(dev); 147 int error; 148 149 sc->sc_udev = uaa->device; 150 sc->sc_unit = device_get_unit(dev); 151 152 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s", 153 device_get_nameunit(dev)); 154 155 mtx_init(&sc->sc_mtx, "ufm lock", NULL, MTX_DEF | MTX_RECURSE); 156 157 device_set_usb_desc(dev); 158 159 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 160 &ufm_fifo_methods, &sc->sc_fifo, 161 device_get_unit(dev), -1, uaa->info.bIfaceIndex, 162 UID_ROOT, GID_OPERATOR, 0644); 163 if (error) { 164 goto detach; 165 } 166 return (0); /* success */ 167 168detach: 169 ufm_detach(dev); 170 return (ENXIO); 171} 172 173static int 174ufm_detach(device_t dev) 175{ 176 struct ufm_softc *sc = device_get_softc(dev); 177 178 usb_fifo_detach(&sc->sc_fifo); 179 180 mtx_destroy(&sc->sc_mtx); 181 182 return (0); 183} 184 185static int 186ufm_do_req(struct ufm_softc *sc, uint8_t request, 187 uint16_t value, uint16_t index, uint8_t *retbuf) 188{ 189 int error; 190 191 struct usb_device_request req; 192 uint8_t buf[1]; 193 194 req.bmRequestType = UT_READ_VENDOR_DEVICE; 195 req.bRequest = request; 196 USETW(req.wValue, value); 197 USETW(req.wIndex, index); 198 USETW(req.wLength, 1); 199 200 error = usbd_do_request(sc->sc_udev, NULL, &req, buf); 201 202 if (retbuf) { 203 *retbuf = buf[0]; 204 } 205 if (error) { 206 return (ENXIO); 207 } 208 return (0); 209} 210 211static int 212ufm_set_freq(struct ufm_softc *sc, void *addr) 213{ 214 int freq = *(int *)addr; 215 216 /* 217 * Freq now is in Hz. We need to convert it to the frequency 218 * that the radio wants. This frequency is 10.7MHz above 219 * the actual frequency. We then need to convert to 220 * units of 12.5kHz. We add one to the IFM to make rounding 221 * easier. 222 */ 223 mtx_lock(&sc->sc_mtx); 224 sc->sc_freq = freq; 225 mtx_unlock(&sc->sc_mtx); 226 227 freq = (freq + 10700001) / 12500; 228 229 /* This appears to set the frequency */ 230 if (ufm_do_req(sc, UFM_CMD_SET_FREQ, 231 freq >> 8, freq, NULL) != 0) { 232 return (EIO); 233 } 234 /* Not sure what this does */ 235 if (ufm_do_req(sc, UFM_CMD0, 236 0x96, 0xb7, NULL) != 0) { 237 return (EIO); 238 } 239 return (0); 240} 241 242static int 243ufm_get_freq(struct ufm_softc *sc, void *addr) 244{ 245 int *valp = (int *)addr; 246 247 mtx_lock(&sc->sc_mtx); 248 *valp = sc->sc_freq; 249 mtx_unlock(&sc->sc_mtx); 250 return (0); 251} 252 253static int 254ufm_start(struct ufm_softc *sc, void *addr) 255{ 256 uint8_t ret; 257 258 if (ufm_do_req(sc, UFM_CMD0, 259 0x00, 0xc7, &ret)) { 260 return (EIO); 261 } 262 if (ufm_do_req(sc, UFM_CMD2, 263 0x01, 0x00, &ret)) { 264 return (EIO); 265 } 266 if (ret & 0x1) { 267 return (EIO); 268 } 269 return (0); 270} 271 272static int 273ufm_stop(struct ufm_softc *sc, void *addr) 274{ 275 if (ufm_do_req(sc, UFM_CMD0, 276 0x16, 0x1C, NULL)) { 277 return (EIO); 278 } 279 if (ufm_do_req(sc, UFM_CMD2, 280 0x00, 0x00, NULL)) { 281 return (EIO); 282 } 283 return (0); 284} 285 286static int 287ufm_get_stat(struct ufm_softc *sc, void *addr) 288{ 289 uint8_t ret; 290 291 /* 292 * Note, there's a 240ms settle time before the status 293 * will be valid, so sleep that amount. 294 */ 295 usb_pause_mtx(NULL, hz / 4); 296 297 if (ufm_do_req(sc, UFM_CMD0, 298 0x00, 0x24, &ret)) { 299 return (EIO); 300 } 301 *(int *)addr = ret; 302 303 return (0); 304} 305 306static int 307ufm_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, 308 int fflags) 309{ 310 struct ufm_softc *sc = usb_fifo_softc(fifo); 311 int error = 0; 312 313 if ((fflags & (FWRITE | FREAD)) != (FWRITE | FREAD)) { 314 return (EACCES); 315 } 316 317 switch (cmd) { 318 case FM_SET_FREQ: 319 error = ufm_set_freq(sc, addr); 320 break; 321 case FM_GET_FREQ: 322 error = ufm_get_freq(sc, addr); 323 break; 324 case FM_START: 325 error = ufm_start(sc, addr); 326 break; 327 case FM_STOP: 328 error = ufm_stop(sc, addr); 329 break; 330 case FM_GET_STAT: 331 error = ufm_get_stat(sc, addr); 332 break; 333 default: 334 error = ENOTTY; 335 break; 336 } 337 return (error); 338} 339