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