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