udsbr.c revision 1.14
1/*	$NetBSD: udsbr.c,v 1.14 2008/04/28 20:23:59 martin 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.14 2008/04/28 20:23:59 martin Exp $");
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/device.h>
47
48#include <sys/radioio.h>
49#include <dev/radio_if.h>
50
51#include <dev/usb/usb.h>
52#include <dev/usb/usbdi.h>
53#include <dev/usb/usbdi_util.h>
54
55#include <dev/usb/usbdevs.h>
56
57#ifdef UDSBR_DEBUG
58#define DPRINTF(x)	if (udsbrdebug) logprintf x
59#define DPRINTFN(n,x)	if (udsbrdebug>(n)) logprintf x
60int	udsbrdebug = 0;
61#else
62#define DPRINTF(x)
63#define DPRINTFN(n,x)
64#endif
65
66#define UDSBR_CONFIG_NO		1
67
68Static	int     udsbr_get_info(void *, struct radio_info *);
69Static	int     udsbr_set_info(void *, struct radio_info *);
70
71const struct radio_hw_if udsbr_hw_if = {
72	NULL, /* open */
73	NULL, /* close */
74	udsbr_get_info,
75	udsbr_set_info,
76	NULL
77};
78
79struct udsbr_softc {
80 	USBBASEDEVICE		sc_dev;
81	usbd_device_handle	sc_udev;
82
83	char			sc_mute;
84	char			sc_vol;
85	u_int32_t		sc_freq;
86
87	struct device		*sc_child;
88
89	char			sc_dying;
90};
91
92Static	int	udsbr_req(struct udsbr_softc *sc, int ureq, int value,
93			  int index);
94Static	void	udsbr_start(struct udsbr_softc *sc);
95Static	void	udsbr_stop(struct udsbr_softc *sc);
96Static	void	udsbr_setfreq(struct udsbr_softc *sc, int freq);
97Static	int	udsbr_status(struct udsbr_softc *sc);
98
99int udsbr_match(device_t, struct cfdata *, void *);
100void udsbr_attach(device_t, device_t, void *);
101void udsbr_childdet(device_t, device_t);
102int udsbr_detach(device_t, int);
103int udsbr_activate(device_t, enum devact);
104extern struct cfdriver udsbr_cd;
105CFATTACH_DECL2(udsbr, sizeof(struct udsbr_softc), udsbr_match,
106    udsbr_attach, udsbr_detach, udsbr_activate, NULL, udsbr_childdet);
107
108USB_MATCH(udsbr)
109{
110	USB_MATCH_START(udsbr, uaa);
111
112	DPRINTFN(50,("udsbr_match\n"));
113
114	if (uaa->vendor != USB_VENDOR_CYPRESS ||
115	    uaa->product != USB_PRODUCT_CYPRESS_FMRADIO)
116		return (UMATCH_NONE);
117	return (UMATCH_VENDOR_PRODUCT);
118}
119
120USB_ATTACH(udsbr)
121{
122	USB_ATTACH_START(udsbr, sc, uaa);
123	usbd_device_handle	dev = uaa->device;
124	char			*devinfop;
125	usbd_status		err;
126
127	DPRINTFN(10,("udsbr_attach: sc=%p\n", sc));
128
129	devinfop = usbd_devinfo_alloc(dev, 0);
130	USB_ATTACH_SETUP;
131	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
132	usbd_devinfo_free(devinfop);
133
134	err = usbd_set_config_no(dev, UDSBR_CONFIG_NO, 1);
135	if (err) {
136		printf("%s: setting config no failed\n",
137		    USBDEVNAME(sc->sc_dev));
138		USB_ATTACH_ERROR_RETURN;
139	}
140
141	sc->sc_udev = dev;
142
143	DPRINTFN(10, ("udsbr_attach: %p\n", sc->sc_udev));
144
145	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
146			   USBDEV(sc->sc_dev));
147
148	sc->sc_child = radio_attach_mi(&udsbr_hw_if, sc, USBDEV(sc->sc_dev));
149
150	USB_ATTACH_SUCCESS_RETURN;
151}
152
153void
154udsbr_childdet(device_t self, device_t child)
155{
156}
157
158USB_DETACH(udsbr)
159{
160	USB_DETACH_START(udsbr, sc);
161	int rv = 0;
162
163	if (sc->sc_child != NULL)
164		rv = config_detach(sc->sc_child, flags);
165
166	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
167			   USBDEV(sc->sc_dev));
168
169	return (rv);
170}
171
172int
173udsbr_activate(device_ptr_t self, enum devact act)
174{
175	struct udsbr_softc *sc = (struct udsbr_softc *)self;
176	int rv = 0;
177
178	switch (act) {
179	case DVACT_ACTIVATE:
180		return (EOPNOTSUPP);
181		break;
182
183	case DVACT_DEACTIVATE:
184		sc->sc_dying = 1;
185		if (sc->sc_child != NULL)
186			rv = config_deactivate(sc->sc_child);
187		break;
188	}
189	return (rv);
190}
191
192int
193udsbr_req(struct udsbr_softc *sc, int ureq, int value, int index)
194{
195	usb_device_request_t req;
196	usbd_status err;
197	u_char data;
198
199	DPRINTFN(1,("udsbr_req: ureq=0x%02x value=0x%04x index=0x%04x\n",
200		    ureq, value, index));
201	req.bmRequestType = UT_READ_VENDOR_DEVICE;
202	req.bRequest = ureq;
203	USETW(req.wValue, value);
204	USETW(req.wIndex, index);
205	USETW(req.wLength, 1);
206	err = usbd_do_request(sc->sc_udev, &req, &data);
207	if (err) {
208		printf("%s: request failed err=%d\n", USBDEVNAME(sc->sc_dev),
209		       err);
210	}
211	return !(data & 1);
212}
213
214void
215udsbr_start(struct udsbr_softc *sc)
216{
217	(void)udsbr_req(sc, 0x00, 0x0000, 0x00c7);
218	(void)udsbr_req(sc, 0x02, 0x0001, 0x0000);
219}
220
221void
222udsbr_stop(struct udsbr_softc *sc)
223{
224	(void)udsbr_req(sc, 0x00, 0x0016, 0x001c);
225	(void)udsbr_req(sc, 0x02, 0x0000, 0x0000);
226}
227
228void
229udsbr_setfreq(struct udsbr_softc *sc, int freq)
230{
231	DPRINTF(("udsbr_setfreq: setfreq=%d\n", freq));
232        /*
233         * Freq now is in Hz.  We need to convert it to the frequency
234         * that the radio wants.  This frequency is 10.7MHz above
235         * the actual frequency.  We then need to convert to
236         * units of 12.5kHz.  We add one to the IFM to make rounding
237         * easier.
238         */
239        freq = (freq * 1000 + 10700001) / 12500;
240	(void)udsbr_req(sc, 0x01, (freq >> 8) & 0xff, freq & 0xff);
241	(void)udsbr_req(sc, 0x00, 0x0096, 0x00b7);
242	usbd_delay_ms(sc->sc_udev, 240); /* wait for signal to settle */
243}
244
245int
246udsbr_status(struct udsbr_softc *sc)
247{
248	return (udsbr_req(sc, 0x00, 0x0000, 0x0024));
249}
250
251
252int
253udsbr_get_info(void *v, struct radio_info *ri)
254{
255	struct udsbr_softc *sc = v;
256
257	ri->mute = sc->sc_mute;
258	ri->volume = sc->sc_vol ? 255 : 0;
259	ri->caps = RADIO_CAPS_DETECT_STEREO;
260	ri->rfreq = 0;
261	ri->lock = 0;
262	ri->freq = sc->sc_freq;
263	ri->info = udsbr_status(sc) ? RADIO_INFO_STEREO : 0;
264
265	return (0);
266}
267
268int
269udsbr_set_info(void *v, struct radio_info *ri)
270{
271	struct udsbr_softc *sc = v;
272
273	sc->sc_mute = ri->mute != 0;
274	sc->sc_vol = ri->volume != 0;
275	sc->sc_freq = ri->freq;
276	udsbr_setfreq(sc, sc->sc_freq);
277
278	if (sc->sc_mute || sc->sc_vol == 0)
279		udsbr_stop(sc);
280	else
281		udsbr_start(sc);
282
283	return (0);
284}
285