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