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