xref: /netbsd-src/sys/dev/usb/slurm.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /*	$NetBSD: slurm.c,v 1.2 2016/04/23 10:15:32 skrll Exp $ */
2 
3 /*
4  * Copyright (c) 2012 Jonathan A. Kollasch
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: slurm.c,v 1.2 2016/04/23 10:15:32 skrll Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/conf.h>
38 
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41 #include <dev/usb/usbdivar.h>
42 #include <dev/usb/usbdi_util.h>
43 #include <dev/usb/usbdevs.h>
44 #include <dev/ic/si470x_reg.h>
45 
46 #include <sys/radioio.h>
47 #include <dev/radio_if.h>
48 
49 #ifdef SLURM_DEBUG
50 int	slurmdebug = 0;
51 #define DPRINTFN(n, x)	do { if (slurmdebug > (n)) printf x; } while (0)
52 #else
53 #define DPRINTFN(n, x)
54 #endif
55 
56 #define DPRINTF(x) DPRINTFN(0, x)
57 
58 #define SI470X_VOLFACT (255 / __SHIFTOUT_MASK(SI470X_VOLUME))
59 
60 struct slurm_softc {
61 	device_t		sc_dev;
62 	struct usbd_device *	sc_udev;
63 	struct usbd_interface *	sc_uif;
64 	uint32_t		sc_band;
65 	uint32_t		sc_space;
66 };
67 
68 static const struct usb_devno slurm_devs[] = {
69 	{ USB_VENDOR_ADS, USB_PRODUCT_ADS_RDX155 },
70 };
71 
72 static int slurm_match(device_t, cfdata_t, void *);
73 static void slurm_attach(device_t, device_t, void *);
74 static int slurm_detach(device_t, int);
75 
76 static int slurm_get_info(void *, struct radio_info *);
77 static int slurm_set_info(void *, struct radio_info *);
78 static int slurm_search(void *, int);
79 
80 static usbd_status slurm_setreg(struct slurm_softc *, int, uint16_t);
81 static usbd_status slurm_getreg(struct slurm_softc *, int, uint16_t *);
82 
83 static uint32_t slurm_si470x_get_freq(struct slurm_softc *, uint16_t);
84 static void slurm_si470x_get_bandspace(struct slurm_softc *, uint16_t);
85 static int slurm_si470x_get_info(uint16_t);
86 static int slurm_si470x_get_mute(uint16_t);
87 static int slurm_si470x_get_stereo(uint16_t);
88 static int slurm_si470x_get_volume(uint16_t);
89 
90 static int slurm_si470x_search(struct slurm_softc *, int);
91 
92 static void slurm_si470x_set_freq(struct slurm_softc *, uint32_t);
93 static void slurm_si470x_set_powercfg(struct slurm_softc *, int, int);
94 static void slurm_si470x_set_volume(struct slurm_softc *, int);
95 
96 static const struct radio_hw_if slurm_radio = {
97 	.get_info = slurm_get_info,
98 	.set_info = slurm_set_info,
99 	.search = slurm_search,
100 };
101 
102 CFATTACH_DECL_NEW(slurm, sizeof(struct slurm_softc),
103     slurm_match, slurm_attach, slurm_detach, NULL);
104 
105 static int
106 slurm_match(device_t parent, cfdata_t match, void *aux)
107 {
108 	const struct usbif_attach_arg * const uiaa = aux;
109 
110 	if (uiaa->uiaa_ifaceno != 2)
111 		return UMATCH_NONE;
112 
113 	if (usb_lookup(slurm_devs, uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL) {
114 		return UMATCH_VENDOR_PRODUCT;
115 	}
116 
117 	return UMATCH_NONE;
118 }
119 
120 static void
121 slurm_attach(device_t parent, device_t self, void *aux)
122 {
123 	struct slurm_softc * const sc = device_private(self);
124 	const struct usbif_attach_arg * const uiaa = aux;
125 
126 	sc->sc_dev = self;
127 	sc->sc_udev = uiaa->uiaa_device;
128 	sc->sc_uif = uiaa->uiaa_iface;
129 
130 	aprint_normal("\n");
131 	aprint_naive("\n");
132 
133 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
134 
135 #ifdef SLURM_DEBUG
136 	{
137 		uint16_t val;
138 		for (int i = 0; i < 16; i++) {
139 			slurm_getreg(sc, i, &val);
140 			device_printf(self, "%02x -> %04x\n", i, val);
141 		}
142 	}
143 #endif
144 
145 	radio_attach_mi(&slurm_radio, sc, self);
146 }
147 
148 static int
149 slurm_detach(device_t self, int flags)
150 {
151 	struct slurm_softc * const sc = device_private(self);
152 	int rv = 0;
153 
154 	if ((rv = config_detach_children(self, flags)) != 0)
155 		return rv;
156 
157 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
158 	    sc->sc_dev);
159 
160 	return (rv);
161 }
162 
163 static int
164 slurm_get_info(void *v, struct radio_info *ri)
165 {
166 	struct slurm_softc * const sc = v;
167 	uint16_t powercfg, sysconfig2, readchannel, statusrssi;
168 
169 	slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
170 	slurm_getreg(sc, SI470X_SYSCONFIG2, &sysconfig2);
171 	slurm_getreg(sc, SI470X_STATUSRSSI, &statusrssi);
172 	slurm_getreg(sc, SI470X_READCHANNEL, &readchannel);
173 
174 	ri->mute = slurm_si470x_get_mute(powercfg);
175 	ri->volume = slurm_si470x_get_volume(sysconfig2);
176 	ri->stereo = slurm_si470x_get_stereo(powercfg);
177 	ri->rfreq = 0;
178 	ri->lock = 0;
179 	slurm_si470x_get_bandspace(sc, sysconfig2);
180 	ri->freq = slurm_si470x_get_freq(sc, readchannel);
181 	ri->caps = RADIO_CAPS_DETECT_STEREO | RADIO_CAPS_DETECT_SIGNAL |
182 		   RADIO_CAPS_SET_MONO | RADIO_CAPS_HW_SEARCH |
183 		   RADIO_CAPS_HW_AFC | RADIO_CAPS_LOCK_SENSITIVITY;
184 	ri->info = slurm_si470x_get_info(statusrssi);
185 
186 	return 0;
187 }
188 
189 static int
190 slurm_set_info(void *v, struct radio_info *ri)
191 {
192 	struct slurm_softc * const sc = v;
193 
194 	slurm_si470x_set_freq(sc, ri->freq);
195 	slurm_si470x_set_powercfg(sc, ri->mute, ri->stereo);
196 	slurm_si470x_set_volume(sc, ri->volume);
197 
198 	return 0;
199 }
200 
201 static int
202 slurm_search(void *v, int f)
203 {
204 	struct slurm_softc * const sc = v;
205 
206 	return slurm_si470x_search(sc, f);
207 }
208 
209 static usbd_status
210 slurm_getreg(struct slurm_softc *sc, int reg, uint16_t *val)
211 {
212 	usbd_status status;
213 	uint8_t s[3];
214 
215 	++reg;
216 
217 	s[0] = reg;
218 	s[1] = s[2] = 0;
219 
220 	status = usbd_get_report(sc->sc_uif, UHID_FEATURE_REPORT,
221 		reg, &s, sizeof(s));
222 
223 	*val = (s[1] << 8) | s[2];
224 
225 	return status;
226 }
227 
228 static usbd_status
229 slurm_setreg(struct slurm_softc *sc, int reg, uint16_t val)
230 {
231 	usbd_status status;
232 	uint8_t s[3];
233 
234 	++reg;
235 
236 	s[0] = reg;
237 	s[1] = (val >> 8) & 0xff;
238 	s[2] = (val >> 0) & 0xff;
239 
240 	status = usbd_set_report(sc->sc_uif, UHID_FEATURE_REPORT,
241 		reg, &s, sizeof(s));
242 
243 	return status;
244 }
245 
246 static int
247 slurm_si470x_await_stc(struct slurm_softc *sc)
248 {
249 	int i;
250 	uint16_t statusrssi;
251 
252 	for (i = 50; i > 0; i--) {
253 		usbd_delay_ms(sc->sc_udev, 2);
254 		slurm_getreg(sc, SI470X_STATUSRSSI, &statusrssi);
255 		if ((statusrssi & (SI470X_STC|SI470X_SF_BL)) != 0)
256 			break;
257 	}
258 
259 	if (i == 0)
260 		return -1;
261 	else
262 		return 0;
263 }
264 
265 static void
266 slurm_si470x_get_bandspace(struct slurm_softc *sc, uint16_t sysconfig2)
267 {
268 	switch (__SHIFTOUT(sysconfig2, SI470X_SPACE)) {
269 	default:
270 	case 0:
271 		sc->sc_space = 200;
272 		break;
273 	case 1:
274 		sc->sc_space = 100;
275 		break;
276 	case 2:
277 		sc->sc_space = 50;
278 		break;
279 	}
280 
281 	switch (__SHIFTOUT(sysconfig2, SI470X_BAND)) {
282 	default:
283 	case 0:
284 		sc->sc_band = 87500;
285 		break;
286 	case 1:
287 	case 2:
288 		sc->sc_band = 76000;
289 		break;
290 	}
291 }
292 
293 static uint32_t
294 slurm_si470x_get_freq(struct slurm_softc *sc, uint16_t readchannel)
295 {
296 	readchannel = __SHIFTOUT(readchannel, SI470X_READCHAN);
297 	return sc->sc_band + readchannel * sc->sc_space;
298 }
299 
300 static int
301 slurm_si470x_get_info(uint16_t statusrssi)
302 {
303 	return (__SHIFTOUT(statusrssi, SI470X_ST) ? RADIO_INFO_STEREO : 0)
304 	    | (__SHIFTOUT(statusrssi, SI470X_AFCRL) ? 0 : RADIO_INFO_SIGNAL);
305 }
306 
307 static int
308 slurm_si470x_get_mute(uint16_t powercfg)
309 {
310 	return __SHIFTOUT(powercfg, SI470X_DMUTE) ? 0 : 1;
311 }
312 
313 static int
314 slurm_si470x_get_stereo(uint16_t powercfg)
315 {
316 	return __SHIFTOUT(powercfg, SI470X_MONO) ? 0 : 1;
317 }
318 
319 static int
320 slurm_si470x_get_volume(uint16_t sysconfig2)
321 {
322 	return __SHIFTOUT(sysconfig2, SI470X_VOLUME) * SI470X_VOLFACT;
323 }
324 
325 static int
326 slurm_si470x_search(struct slurm_softc *sc, int up)
327 {
328 	uint16_t powercfg;
329 
330 	slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
331 	powercfg &= ~(SI470X_SKMODE|SI470X_SEEKUP|SI470X_SEEK);
332 	powercfg |= up ? SI470X_SEEKUP : 0;
333 	slurm_setreg(sc, SI470X_POWERCFG, SI470X_SEEK|powercfg);
334 	slurm_si470x_await_stc(sc);
335 	slurm_setreg(sc, SI470X_POWERCFG, powercfg);
336 
337 	return 0;
338 }
339 
340 static void
341 slurm_si470x_set_freq(struct slurm_softc *sc, uint32_t freq)
342 {
343 	uint16_t channel;
344 
345 	channel = (freq - sc->sc_band) / sc->sc_space;
346 
347 	slurm_setreg(sc, SI470X_CHANNEL, SI470X_TUNE|channel);
348 	slurm_si470x_await_stc(sc);
349 	slurm_setreg(sc, SI470X_CHANNEL, channel);
350 
351 #ifdef SLURM_DEBUG
352 	device_printf(sc->sc_dev, "%s 0a -> %04x after %d\n", __func__, val, i);
353 #endif
354 }
355 
356 static void
357 slurm_si470x_set_powercfg(struct slurm_softc *sc, int mute, int stereo)
358 {
359 	uint16_t powercfg;
360 
361 	slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
362 	powercfg &= ~(SI470X_DMUTE|SI470X_MONO);
363 	powercfg |= SI470X_DSMUTE;
364 	powercfg |= mute ? 0 : SI470X_DMUTE;
365 	powercfg |= stereo ? 0 : SI470X_MONO;
366 	slurm_setreg(sc, SI470X_POWERCFG, powercfg);
367 }
368 
369 static void
370 slurm_si470x_set_volume(struct slurm_softc *sc, int volume)
371 {
372 	uint16_t sysconfig2;
373 
374 	slurm_getreg(sc, SI470X_SYSCONFIG2, &sysconfig2);
375 	sysconfig2 &= ~SI470X_VOLUME;
376 	sysconfig2 |= __SHIFTIN(volume / SI470X_VOLFACT, SI470X_VOLUME);
377 	slurm_setreg(sc, SI470X_SYSCONFIG2, sysconfig2);
378 }
379