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