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