udsbr.c revision 1.31
1/*	$NetBSD: udsbr.c,v 1.31 2020/03/14 02:35:33 christos Exp $	*/
2
3/*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Driver for the D-Link DSB-R100 FM radio.
34 * I apologize for the magic hex constants, but this is what happens
35 * when you have to reverse engineer the driver.
36 * Parts of the code borrowed from Linux and parts from Warner Losh's
37 * FreeBSD driver.
38 */
39
40#include <sys/cdefs.h>
41__KERNEL_RCSID(0, "$NetBSD: udsbr.c,v 1.31 2020/03/14 02:35:33 christos Exp $");
42
43#ifdef _KERNEL_OPT
44#include "opt_usb.h"
45#endif
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/device.h>
51
52#include <sys/radioio.h>
53#include <dev/radio_if.h>
54
55#include <dev/usb/usb.h>
56#include <dev/usb/usbdi.h>
57#include <dev/usb/usbdi_util.h>
58#include <dev/usb/usbdivar.h>
59
60#include <dev/usb/usbdevs.h>
61
62#ifdef UDSBR_DEBUG
63#define DPRINTF(x)	if (udsbrdebug) printf x
64#define DPRINTFN(n,x)	if (udsbrdebug>(n)) printf x
65int	udsbrdebug = 0;
66#else
67#define DPRINTF(x)
68#define DPRINTFN(n,x)
69#endif
70
71#define UDSBR_CONFIG_NO		1
72
73Static	int     udsbr_get_info(void *, struct radio_info *);
74Static	int     udsbr_set_info(void *, struct radio_info *);
75
76static const struct radio_hw_if udsbr_hw_if = {
77	NULL, /* open */
78	NULL, /* close */
79	udsbr_get_info,
80	udsbr_set_info,
81	NULL
82};
83
84struct udsbr_softc {
85	device_t		sc_dev;
86	struct usbd_device *	sc_udev;
87
88	char			sc_mute;
89	char			sc_vol;
90	uint32_t		sc_freq;
91
92	device_t		sc_child;
93
94	char			sc_dying;
95};
96
97Static	int	udsbr_req(struct udsbr_softc *, int, int,
98			  int);
99Static	void	udsbr_start(struct udsbr_softc *);
100Static	void	udsbr_stop(struct udsbr_softc *);
101Static	void	udsbr_setfreq(struct udsbr_softc *, int);
102Static	int	udsbr_status(struct udsbr_softc *);
103
104static int udsbr_match(device_t, cfdata_t, void *);
105static void udsbr_attach(device_t, device_t, void *);
106static void udsbr_childdet(device_t, device_t);
107static int udsbr_detach(device_t, int);
108static int udsbr_activate(device_t, enum devact);
109
110CFATTACH_DECL2_NEW(udsbr, sizeof(struct udsbr_softc), udsbr_match,
111    udsbr_attach, udsbr_detach, udsbr_activate, NULL, udsbr_childdet);
112
113static int
114udsbr_match(device_t parent, cfdata_t match, void *aux)
115{
116	struct usb_attach_arg *uaa = aux;
117
118	DPRINTFN(50,("udsbr_match\n"));
119
120	if (uaa->uaa_vendor != USB_VENDOR_CYPRESS ||
121	    uaa->uaa_product != USB_PRODUCT_CYPRESS_FMRADIO)
122		return UMATCH_NONE;
123	return UMATCH_VENDOR_PRODUCT;
124}
125
126static void
127udsbr_attach(device_t parent, device_t self, void *aux)
128{
129	struct udsbr_softc *sc = device_private(self);
130	struct usb_attach_arg *uaa = aux;
131	struct usbd_device *	dev = uaa->uaa_device;
132	char			*devinfop;
133	usbd_status		err;
134
135	DPRINTFN(10,("udsbr_attach: sc=%p\n", sc));
136
137	sc->sc_dev = self;
138
139	aprint_naive("\n");
140	aprint_normal("\n");
141
142	devinfop = usbd_devinfo_alloc(dev, 0);
143	aprint_normal_dev(self, "%s\n", devinfop);
144	usbd_devinfo_free(devinfop);
145
146	err = usbd_set_config_no(dev, UDSBR_CONFIG_NO, 1);
147	if (err) {
148		aprint_error_dev(self, "failed to set configuration"
149		    ", err=%s\n", usbd_errstr(err));
150		return;
151	}
152
153	sc->sc_udev = dev;
154
155	DPRINTFN(10, ("udsbr_attach: %p\n", sc->sc_udev));
156
157	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
158
159	sc->sc_child = radio_attach_mi(&udsbr_hw_if, sc, sc->sc_dev);
160
161	return;
162}
163
164static void
165udsbr_childdet(device_t self, device_t child)
166{
167}
168
169static int
170udsbr_detach(device_t self, int flags)
171{
172	struct udsbr_softc *sc = device_private(self);
173	int rv = 0;
174
175	if (sc->sc_child != NULL)
176		rv = config_detach(sc->sc_child, flags);
177	if (sc->sc_udev != NULL)
178		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
179		    sc->sc_dev);
180
181	return rv;
182}
183
184static int
185udsbr_activate(device_t self, enum devact act)
186{
187	struct udsbr_softc *sc = device_private(self);
188
189	switch (act) {
190	case DVACT_DEACTIVATE:
191		sc->sc_dying = 1;
192		return 0;
193	default:
194		return EOPNOTSUPP;
195	}
196}
197
198int
199udsbr_req(struct udsbr_softc *sc, int ureq, int value, int index)
200{
201	usb_device_request_t req;
202	usbd_status err;
203	u_char data;
204
205	DPRINTFN(1,("udsbr_req: ureq=0x%02x value=0x%04x index=0x%04x\n",
206		    ureq, value, index));
207	req.bmRequestType = UT_READ_VENDOR_DEVICE;
208	req.bRequest = ureq;
209	USETW(req.wValue, value);
210	USETW(req.wIndex, index);
211	USETW(req.wLength, 1);
212	err = usbd_do_request(sc->sc_udev, &req, &data);
213	if (err) {
214		aprint_error_dev(sc->sc_dev, "request failed err=%d\n", err);
215	}
216	return !(data & 1);
217}
218
219void
220udsbr_start(struct udsbr_softc *sc)
221{
222	(void)udsbr_req(sc, 0x00, 0x0000, 0x00c7);
223	(void)udsbr_req(sc, 0x02, 0x0001, 0x0000);
224}
225
226void
227udsbr_stop(struct udsbr_softc *sc)
228{
229	(void)udsbr_req(sc, 0x00, 0x0016, 0x001c);
230	(void)udsbr_req(sc, 0x02, 0x0000, 0x0000);
231}
232
233void
234udsbr_setfreq(struct udsbr_softc *sc, int freq)
235{
236	DPRINTF(("udsbr_setfreq: setfreq=%d\n", freq));
237	/*
238	 * Freq now is in Hz.  We need to convert it to the frequency
239	 * that the radio wants.  This frequency is 10.7MHz above
240	 * the actual frequency.  We then need to convert to
241	 * units of 12.5kHz.  We add one to the IFM to make rounding
242	 * easier.
243	 */
244	freq = (freq * 1000 + 10700001) / 12500;
245	(void)udsbr_req(sc, 0x01, (freq >> 8) & 0xff, freq & 0xff);
246	(void)udsbr_req(sc, 0x00, 0x0096, 0x00b7);
247	usbd_delay_ms(sc->sc_udev, 240); /* wait for signal to settle */
248}
249
250int
251udsbr_status(struct udsbr_softc *sc)
252{
253	return udsbr_req(sc, 0x00, 0x0000, 0x0024);
254}
255
256
257int
258udsbr_get_info(void *v, struct radio_info *ri)
259{
260	struct udsbr_softc *sc = v;
261
262	ri->mute = sc->sc_mute;
263	ri->volume = sc->sc_vol ? 255 : 0;
264	ri->caps = RADIO_CAPS_DETECT_STEREO;
265	ri->rfreq = 0;
266	ri->lock = 0;
267	ri->freq = sc->sc_freq;
268	ri->info = udsbr_status(sc) ? RADIO_INFO_STEREO : 0;
269
270	return 0;
271}
272
273int
274udsbr_set_info(void *v, struct radio_info *ri)
275{
276	struct udsbr_softc *sc = v;
277
278	sc->sc_mute = ri->mute != 0;
279	sc->sc_vol = ri->volume != 0;
280	sc->sc_freq = ri->freq;
281	udsbr_setfreq(sc, sc->sc_freq);
282
283	if (sc->sc_mute || sc->sc_vol == 0)
284		udsbr_stop(sc);
285	else
286		udsbr_start(sc);
287
288	return 0;
289}
290