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
71struct 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
85static device_probe_t ufm_probe;
86static device_attach_t ufm_attach;
87static device_detach_t ufm_detach;
88
89static usb_fifo_ioctl_t ufm_ioctl;
90
91static struct usb_fifo_methods ufm_fifo_methods = {
92	.f_ioctl = &ufm_ioctl,
93	.basename[0] = "ufm",
94};
95
96static int	ufm_do_req(struct ufm_softc *, uint8_t, uint16_t, uint16_t,
97		    uint8_t *);
98static int	ufm_set_freq(struct ufm_softc *, void *);
99static int	ufm_get_freq(struct ufm_softc *, void *);
100static int	ufm_start(struct ufm_softc *, void *);
101static int	ufm_stop(struct ufm_softc *, void *);
102static int	ufm_get_stat(struct ufm_softc *, void *);
103
104static devclass_t ufm_devclass;
105
106static 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
114static driver_t ufm_driver = {
115	.name = "ufm",
116	.methods = ufm_methods,
117	.size = sizeof(struct ufm_softc),
118};
119
120static const STRUCT_USB_HOST_ID ufm_devs[] = {
121	{USB_VPI(USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_FMRADIO, 0)},
122};
123
124DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, NULL, 0);
125MODULE_DEPEND(ufm, usb, 1, 1, 1);
126MODULE_VERSION(ufm, 1);
127USB_PNP_HOST_INFO(ufm_devs);
128
129static int
130ufm_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
144static int
145ufm_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	gone_in_dev(dev, 13, "Driver no longer relevant");
169	return (0);			/* success */
170
171detach:
172	ufm_detach(dev);
173	return (ENXIO);
174}
175
176static int
177ufm_detach(device_t dev)
178{
179	struct ufm_softc *sc = device_get_softc(dev);
180
181	usb_fifo_detach(&sc->sc_fifo);
182
183	mtx_destroy(&sc->sc_mtx);
184
185	return (0);
186}
187
188static int
189ufm_do_req(struct ufm_softc *sc, uint8_t request,
190    uint16_t value, uint16_t index, uint8_t *retbuf)
191{
192	int error;
193
194	struct usb_device_request req;
195	uint8_t buf[1];
196
197	req.bmRequestType = UT_READ_VENDOR_DEVICE;
198	req.bRequest = request;
199	USETW(req.wValue, value);
200	USETW(req.wIndex, index);
201	USETW(req.wLength, 1);
202
203	error = usbd_do_request(sc->sc_udev, NULL, &req, buf);
204
205	if (retbuf) {
206		*retbuf = buf[0];
207	}
208	if (error) {
209		return (ENXIO);
210	}
211	return (0);
212}
213
214static int
215ufm_set_freq(struct ufm_softc *sc, void *addr)
216{
217	int freq = *(int *)addr;
218
219	/*
220	 * Freq now is in Hz.  We need to convert it to the frequency
221	 * that the radio wants.  This frequency is 10.7MHz above
222	 * the actual frequency.  We then need to convert to
223	 * units of 12.5kHz.  We add one to the IFM to make rounding
224	 * easier.
225	 */
226	mtx_lock(&sc->sc_mtx);
227	sc->sc_freq = freq;
228	mtx_unlock(&sc->sc_mtx);
229
230	freq = (freq + 10700001) / 12500;
231
232	/* This appears to set the frequency */
233	if (ufm_do_req(sc, UFM_CMD_SET_FREQ,
234	    freq >> 8, freq, NULL) != 0) {
235		return (EIO);
236	}
237	/* Not sure what this does */
238	if (ufm_do_req(sc, UFM_CMD0,
239	    0x96, 0xb7, NULL) != 0) {
240		return (EIO);
241	}
242	return (0);
243}
244
245static int
246ufm_get_freq(struct ufm_softc *sc, void *addr)
247{
248	int *valp = (int *)addr;
249
250	mtx_lock(&sc->sc_mtx);
251	*valp = sc->sc_freq;
252	mtx_unlock(&sc->sc_mtx);
253	return (0);
254}
255
256static int
257ufm_start(struct ufm_softc *sc, void *addr)
258{
259	uint8_t ret;
260
261	if (ufm_do_req(sc, UFM_CMD0,
262	    0x00, 0xc7, &ret)) {
263		return (EIO);
264	}
265	if (ufm_do_req(sc, UFM_CMD2,
266	    0x01, 0x00, &ret)) {
267		return (EIO);
268	}
269	if (ret & 0x1) {
270		return (EIO);
271	}
272	return (0);
273}
274
275static int
276ufm_stop(struct ufm_softc *sc, void *addr)
277{
278	if (ufm_do_req(sc, UFM_CMD0,
279	    0x16, 0x1C, NULL)) {
280		return (EIO);
281	}
282	if (ufm_do_req(sc, UFM_CMD2,
283	    0x00, 0x00, NULL)) {
284		return (EIO);
285	}
286	return (0);
287}
288
289static int
290ufm_get_stat(struct ufm_softc *sc, void *addr)
291{
292	uint8_t ret;
293
294	/*
295	 * Note, there's a 240ms settle time before the status
296	 * will be valid, so sleep that amount.
297	 */
298	usb_pause_mtx(NULL, hz / 4);
299
300	if (ufm_do_req(sc, UFM_CMD0,
301	    0x00, 0x24, &ret)) {
302		return (EIO);
303	}
304	*(int *)addr = ret;
305
306	return (0);
307}
308
309static int
310ufm_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
311    int fflags)
312{
313	struct ufm_softc *sc = usb_fifo_softc(fifo);
314	int error = 0;
315
316	if ((fflags & (FWRITE | FREAD)) != (FWRITE | FREAD)) {
317		return (EACCES);
318	}
319
320	switch (cmd) {
321	case FM_SET_FREQ:
322		error = ufm_set_freq(sc, addr);
323		break;
324	case FM_GET_FREQ:
325		error = ufm_get_freq(sc, addr);
326		break;
327	case FM_START:
328		error = ufm_start(sc, addr);
329		break;
330	case FM_STOP:
331		error = ufm_stop(sc, addr);
332		break;
333	case FM_GET_STAT:
334		error = ufm_get_stat(sc, addr);
335		break;
336	default:
337		error = ENOTTY;
338		break;
339	}
340	return (error);
341}
342