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