xref: /netbsd-src/sys/dev/usb/udsbr.c (revision 2e2322c9c07009df921d11b1268f8506affbb8ba)
1 /*	$NetBSD: udsbr.c,v 1.26 2016/12/04 10:12:35 skrll 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.26 2016/12/04 10:12:35 skrll 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
65 int	udsbrdebug = 0;
66 #else
67 #define DPRINTF(x)
68 #define DPRINTFN(n,x)
69 #endif
70 
71 #define UDSBR_CONFIG_NO		1
72 
73 Static	int     udsbr_get_info(void *, struct radio_info *);
74 Static	int     udsbr_set_info(void *, struct radio_info *);
75 
76 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 
84 struct 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 
97 Static	int	udsbr_req(struct udsbr_softc *, int, int,
98 			  int);
99 Static	void	udsbr_start(struct udsbr_softc *);
100 Static	void	udsbr_stop(struct udsbr_softc *);
101 Static	void	udsbr_setfreq(struct udsbr_softc *, int);
102 Static	int	udsbr_status(struct udsbr_softc *);
103 
104 int udsbr_match(device_t, cfdata_t, void *);
105 void udsbr_attach(device_t, device_t, void *);
106 void udsbr_childdet(device_t, device_t);
107 int udsbr_detach(device_t, int);
108 int udsbr_activate(device_t, enum devact);
109 extern struct cfdriver udsbr_cd;
110 CFATTACH_DECL2_NEW(udsbr, sizeof(struct udsbr_softc), udsbr_match,
111     udsbr_attach, udsbr_detach, udsbr_activate, NULL, udsbr_childdet);
112 
113 int
114 udsbr_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 
126 void
127 udsbr_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 
164 void
165 udsbr_childdet(device_t self, device_t child)
166 {
167 }
168 
169 int
170 udsbr_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 
178 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
179 
180 	return rv;
181 }
182 
183 int
184 udsbr_activate(device_t self, enum devact act)
185 {
186 	struct udsbr_softc *sc = device_private(self);
187 
188 	switch (act) {
189 	case DVACT_DEACTIVATE:
190 		sc->sc_dying = 1;
191 		return 0;
192 	default:
193 		return EOPNOTSUPP;
194 	}
195 }
196 
197 int
198 udsbr_req(struct udsbr_softc *sc, int ureq, int value, int index)
199 {
200 	usb_device_request_t req;
201 	usbd_status err;
202 	u_char data;
203 
204 	DPRINTFN(1,("udsbr_req: ureq=0x%02x value=0x%04x index=0x%04x\n",
205 		    ureq, value, index));
206 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
207 	req.bRequest = ureq;
208 	USETW(req.wValue, value);
209 	USETW(req.wIndex, index);
210 	USETW(req.wLength, 1);
211 	err = usbd_do_request(sc->sc_udev, &req, &data);
212 	if (err) {
213 		aprint_error_dev(sc->sc_dev, "request failed err=%d\n", err);
214 	}
215 	return !(data & 1);
216 }
217 
218 void
219 udsbr_start(struct udsbr_softc *sc)
220 {
221 	(void)udsbr_req(sc, 0x00, 0x0000, 0x00c7);
222 	(void)udsbr_req(sc, 0x02, 0x0001, 0x0000);
223 }
224 
225 void
226 udsbr_stop(struct udsbr_softc *sc)
227 {
228 	(void)udsbr_req(sc, 0x00, 0x0016, 0x001c);
229 	(void)udsbr_req(sc, 0x02, 0x0000, 0x0000);
230 }
231 
232 void
233 udsbr_setfreq(struct udsbr_softc *sc, int freq)
234 {
235 	DPRINTF(("udsbr_setfreq: setfreq=%d\n", freq));
236 	/*
237 	 * Freq now is in Hz.  We need to convert it to the frequency
238 	 * that the radio wants.  This frequency is 10.7MHz above
239 	 * the actual frequency.  We then need to convert to
240 	 * units of 12.5kHz.  We add one to the IFM to make rounding
241 	 * easier.
242 	 */
243 	freq = (freq * 1000 + 10700001) / 12500;
244 	(void)udsbr_req(sc, 0x01, (freq >> 8) & 0xff, freq & 0xff);
245 	(void)udsbr_req(sc, 0x00, 0x0096, 0x00b7);
246 	usbd_delay_ms(sc->sc_udev, 240); /* wait for signal to settle */
247 }
248 
249 int
250 udsbr_status(struct udsbr_softc *sc)
251 {
252 	return udsbr_req(sc, 0x00, 0x0000, 0x0024);
253 }
254 
255 
256 int
257 udsbr_get_info(void *v, struct radio_info *ri)
258 {
259 	struct udsbr_softc *sc = v;
260 
261 	ri->mute = sc->sc_mute;
262 	ri->volume = sc->sc_vol ? 255 : 0;
263 	ri->caps = RADIO_CAPS_DETECT_STEREO;
264 	ri->rfreq = 0;
265 	ri->lock = 0;
266 	ri->freq = sc->sc_freq;
267 	ri->info = udsbr_status(sc) ? RADIO_INFO_STEREO : 0;
268 
269 	return 0;
270 }
271 
272 int
273 udsbr_set_info(void *v, struct radio_info *ri)
274 {
275 	struct udsbr_softc *sc = v;
276 
277 	sc->sc_mute = ri->mute != 0;
278 	sc->sc_vol = ri->volume != 0;
279 	sc->sc_freq = ri->freq;
280 	udsbr_setfreq(sc, sc->sc_freq);
281 
282 	if (sc->sc_mute || sc->sc_vol == 0)
283 		udsbr_stop(sc);
284 	else
285 		udsbr_start(sc);
286 
287 	return 0;
288 }
289