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