]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/misc/ufm.c
Regularize my copyright notice
[FreeBSD/FreeBSD.git] / sys / dev / usb / misc / ufm.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 M. Warner Losh <imp@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson.
28  * This code includes software developed by the NetBSD Foundation, Inc. and
29  * its contributors.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/bus.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
90 static struct usb_fifo_methods ufm_fifo_methods = {
91         .f_ioctl = &ufm_ioctl,
92         .basename[0] = "ufm",
93 };
94
95 static int      ufm_do_req(struct ufm_softc *, uint8_t, uint16_t, uint16_t,
96                     uint8_t *);
97 static int      ufm_set_freq(struct ufm_softc *, void *);
98 static int      ufm_get_freq(struct ufm_softc *, void *);
99 static int      ufm_start(struct ufm_softc *, void *);
100 static int      ufm_stop(struct ufm_softc *, void *);
101 static int      ufm_get_stat(struct ufm_softc *, void *);
102
103 static devclass_t ufm_devclass;
104
105 static device_method_t ufm_methods[] = {
106         DEVMETHOD(device_probe, ufm_probe),
107         DEVMETHOD(device_attach, ufm_attach),
108         DEVMETHOD(device_detach, ufm_detach),
109
110         DEVMETHOD_END
111 };
112
113 static driver_t ufm_driver = {
114         .name = "ufm",
115         .methods = ufm_methods,
116         .size = sizeof(struct ufm_softc),
117 };
118
119 static const STRUCT_USB_HOST_ID ufm_devs[] = {
120         {USB_VPI(USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_FMRADIO, 0)},
121 };
122
123 DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, NULL, 0);
124 MODULE_DEPEND(ufm, usb, 1, 1, 1);
125 MODULE_VERSION(ufm, 1);
126 USB_PNP_HOST_INFO(ufm_devs);
127
128 static int
129 ufm_probe(device_t dev)
130 {
131         struct usb_attach_arg *uaa = device_get_ivars(dev);
132
133         if (uaa->usb_mode != USB_MODE_HOST)
134                 return (ENXIO);
135         if (uaa->info.bConfigIndex != 0)
136                 return (ENXIO);
137         if (uaa->info.bIfaceIndex != 0)
138                 return (ENXIO);
139
140         return (usbd_lookup_id_by_uaa(ufm_devs, sizeof(ufm_devs), uaa));
141 }
142
143 static int
144 ufm_attach(device_t dev)
145 {
146         struct usb_attach_arg *uaa = device_get_ivars(dev);
147         struct ufm_softc *sc = device_get_softc(dev);
148         int error;
149
150         sc->sc_udev = uaa->device;
151         sc->sc_unit = device_get_unit(dev);
152
153         snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
154             device_get_nameunit(dev));
155
156         mtx_init(&sc->sc_mtx, "ufm lock", NULL, MTX_DEF | MTX_RECURSE);
157
158         device_set_usb_desc(dev);
159
160         error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
161             &ufm_fifo_methods, &sc->sc_fifo,
162             device_get_unit(dev), -1, uaa->info.bIfaceIndex,
163             UID_ROOT, GID_OPERATOR, 0644);
164         if (error) {
165                 goto detach;
166         }
167         return (0);                     /* success */
168
169 detach:
170         ufm_detach(dev);
171         return (ENXIO);
172 }
173
174 static int
175 ufm_detach(device_t dev)
176 {
177         struct ufm_softc *sc = device_get_softc(dev);
178
179         usb_fifo_detach(&sc->sc_fifo);
180
181         mtx_destroy(&sc->sc_mtx);
182
183         return (0);
184 }
185
186 static int
187 ufm_do_req(struct ufm_softc *sc, uint8_t request,
188     uint16_t value, uint16_t index, uint8_t *retbuf)
189 {
190         int error;
191
192         struct usb_device_request req;
193         uint8_t buf[1];
194
195         req.bmRequestType = UT_READ_VENDOR_DEVICE;
196         req.bRequest = request;
197         USETW(req.wValue, value);
198         USETW(req.wIndex, index);
199         USETW(req.wLength, 1);
200
201         error = usbd_do_request(sc->sc_udev, NULL, &req, buf);
202
203         if (retbuf) {
204                 *retbuf = buf[0];
205         }
206         if (error) {
207                 return (ENXIO);
208         }
209         return (0);
210 }
211
212 static int
213 ufm_set_freq(struct ufm_softc *sc, void *addr)
214 {
215         int freq = *(int *)addr;
216
217         /*
218          * Freq now is in Hz.  We need to convert it to the frequency
219          * that the radio wants.  This frequency is 10.7MHz above
220          * the actual frequency.  We then need to convert to
221          * units of 12.5kHz.  We add one to the IFM to make rounding
222          * easier.
223          */
224         mtx_lock(&sc->sc_mtx);
225         sc->sc_freq = freq;
226         mtx_unlock(&sc->sc_mtx);
227
228         freq = (freq + 10700001) / 12500;
229
230         /* This appears to set the frequency */
231         if (ufm_do_req(sc, UFM_CMD_SET_FREQ,
232             freq >> 8, freq, NULL) != 0) {
233                 return (EIO);
234         }
235         /* Not sure what this does */
236         if (ufm_do_req(sc, UFM_CMD0,
237             0x96, 0xb7, NULL) != 0) {
238                 return (EIO);
239         }
240         return (0);
241 }
242
243 static int
244 ufm_get_freq(struct ufm_softc *sc, void *addr)
245 {
246         int *valp = (int *)addr;
247
248         mtx_lock(&sc->sc_mtx);
249         *valp = sc->sc_freq;
250         mtx_unlock(&sc->sc_mtx);
251         return (0);
252 }
253
254 static int
255 ufm_start(struct ufm_softc *sc, void *addr)
256 {
257         uint8_t ret;
258
259         if (ufm_do_req(sc, UFM_CMD0,
260             0x00, 0xc7, &ret)) {
261                 return (EIO);
262         }
263         if (ufm_do_req(sc, UFM_CMD2,
264             0x01, 0x00, &ret)) {
265                 return (EIO);
266         }
267         if (ret & 0x1) {
268                 return (EIO);
269         }
270         return (0);
271 }
272
273 static int
274 ufm_stop(struct ufm_softc *sc, void *addr)
275 {
276         if (ufm_do_req(sc, UFM_CMD0,
277             0x16, 0x1C, NULL)) {
278                 return (EIO);
279         }
280         if (ufm_do_req(sc, UFM_CMD2,
281             0x00, 0x00, NULL)) {
282                 return (EIO);
283         }
284         return (0);
285 }
286
287 static int
288 ufm_get_stat(struct ufm_softc *sc, void *addr)
289 {
290         uint8_t ret;
291
292         /*
293          * Note, there's a 240ms settle time before the status
294          * will be valid, so sleep that amount.
295          */
296         usb_pause_mtx(NULL, hz / 4);
297
298         if (ufm_do_req(sc, UFM_CMD0,
299             0x00, 0x24, &ret)) {
300                 return (EIO);
301         }
302         *(int *)addr = ret;
303
304         return (0);
305 }
306
307 static int
308 ufm_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
309     int fflags)
310 {
311         struct ufm_softc *sc = usb_fifo_softc(fifo);
312         int error = 0;
313
314         if ((fflags & (FWRITE | FREAD)) != (FWRITE | FREAD)) {
315                 return (EACCES);
316         }
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 }