slurm.c revision 1.1.6.2 1 /* $NetBSD: slurm.c,v 1.1.6.2 2013/02/25 00:29:37 tls 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.1.6.2 2013/02/25 00:29:37 tls 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/malloc.h>
37 #include <sys/device.h>
38 #include <sys/conf.h>
39
40 #include <dev/usb/usb.h>
41 #include <dev/usb/usbdi.h>
42 #include <dev/usb/usbdivar.h>
43 #include <dev/usb/usbdi_util.h>
44 #include <dev/usb/usbdevs.h>
45 #include <dev/ic/si470x_reg.h>
46
47 #include <sys/radioio.h>
48 #include <dev/radio_if.h>
49
50 #ifdef SLURM_DEBUG
51 int slurmdebug = 0;
52 #define DPRINTFN(n, x) do { if (slurmdebug > (n)) printf x; } while (0)
53 #else
54 #define DPRINTFN(n, x)
55 #endif
56
57 #define DPRINTF(x) DPRINTFN(0, x)
58
59 #define SI470X_VOLFACT (255 / __SHIFTOUT_MASK(SI470X_VOLUME))
60
61 struct slurm_softc {
62 device_t sc_dev;
63 usbd_device_handle sc_udev;
64 usbd_interface_handle sc_uif;
65 uint32_t sc_band;
66 uint32_t sc_space;
67 };
68
69 static const struct usb_devno slurm_devs[] = {
70 { USB_VENDOR_ADS, USB_PRODUCT_ADS_RDX155 },
71 };
72
73 static int slurm_match(device_t, cfdata_t, void *);
74 static void slurm_attach(device_t, device_t, void *);
75 static int slurm_detach(device_t, int);
76
77 static int slurm_get_info(void *, struct radio_info *);
78 static int slurm_set_info(void *, struct radio_info *);
79 static int slurm_search(void *, int);
80
81 static usbd_status slurm_setreg(struct slurm_softc *, int, uint16_t);
82 static usbd_status slurm_getreg(struct slurm_softc *, int, uint16_t *);
83
84 static uint32_t slurm_si470x_get_freq(struct slurm_softc *, uint16_t);
85 static void slurm_si470x_get_bandspace(struct slurm_softc *, uint16_t);
86 static int slurm_si470x_get_info(uint16_t);
87 static int slurm_si470x_get_mute(uint16_t);
88 static int slurm_si470x_get_stereo(uint16_t);
89 static int slurm_si470x_get_volume(uint16_t);
90
91 static int slurm_si470x_search(struct slurm_softc *, int);
92
93 static void slurm_si470x_set_freq(struct slurm_softc *, uint32_t);
94 static void slurm_si470x_set_powercfg(struct slurm_softc *, int, int);
95 static void slurm_si470x_set_volume(struct slurm_softc *, int);
96
97 static const struct radio_hw_if slurm_radio = {
98 .get_info = slurm_get_info,
99 .set_info = slurm_set_info,
100 .search = slurm_search,
101 };
102
103 CFATTACH_DECL_NEW(slurm, sizeof(struct slurm_softc),
104 slurm_match, slurm_attach, slurm_detach, NULL);
105
106 static int
107 slurm_match(device_t parent, cfdata_t match, void *aux)
108 {
109 const struct usbif_attach_arg * const uaa = aux;
110
111 if (uaa->ifaceno != 2)
112 return UMATCH_NONE;
113
114 if (usb_lookup(slurm_devs, uaa->vendor, uaa->product) != NULL) {
115 return UMATCH_VENDOR_PRODUCT;
116 }
117
118 return UMATCH_NONE;
119 }
120
121 static void
122 slurm_attach(device_t parent, device_t self, void *aux)
123 {
124 struct slurm_softc * const sc = device_private(self);
125 const struct usbif_attach_arg * const uaa = aux;
126
127 sc->sc_dev = self;
128 sc->sc_udev = uaa->device;
129 sc->sc_uif = uaa->iface;
130
131 aprint_normal("\n");
132 aprint_naive("\n");
133
134 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
135
136 #ifdef SLURM_DEBUG
137 {
138 uint16_t val;
139 for (int i = 0; i < 16; i++) {
140 slurm_getreg(sc, i, &val);
141 device_printf(self, "%02x -> %04x\n", i, val);
142 }
143 }
144 #endif
145
146 radio_attach_mi(&slurm_radio, sc, self);
147 }
148
149 static int
150 slurm_detach(device_t self, int flags)
151 {
152 struct slurm_softc * const sc = device_private(self);
153 int rv = 0;
154
155 if ((rv = config_detach_children(self, flags)) != 0)
156 return rv;
157
158 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
159 sc->sc_dev);
160
161 return (rv);
162 }
163
164 static int
165 slurm_get_info(void *v, struct radio_info *ri)
166 {
167 struct slurm_softc * const sc = v;
168 uint16_t powercfg, sysconfig2, readchannel, statusrssi;
169
170 slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
171 slurm_getreg(sc, SI470X_SYSCONFIG2, &sysconfig2);
172 slurm_getreg(sc, SI470X_STATUSRSSI, &statusrssi);
173 slurm_getreg(sc, SI470X_READCHANNEL, &readchannel);
174
175 ri->mute = slurm_si470x_get_mute(powercfg);
176 ri->volume = slurm_si470x_get_volume(sysconfig2);
177 ri->stereo = slurm_si470x_get_stereo(powercfg);
178 ri->rfreq = 0;
179 ri->lock = 0;
180 slurm_si470x_get_bandspace(sc, sysconfig2);
181 ri->freq = slurm_si470x_get_freq(sc, readchannel);
182 ri->caps = RADIO_CAPS_DETECT_STEREO | RADIO_CAPS_DETECT_SIGNAL |
183 RADIO_CAPS_SET_MONO | RADIO_CAPS_HW_SEARCH |
184 RADIO_CAPS_HW_AFC | RADIO_CAPS_LOCK_SENSITIVITY;
185 ri->info = slurm_si470x_get_info(statusrssi);
186
187 return 0;
188 }
189
190 static int
191 slurm_set_info(void *v, struct radio_info *ri)
192 {
193 struct slurm_softc * const sc = v;
194
195 slurm_si470x_set_freq(sc, ri->freq);
196 slurm_si470x_set_powercfg(sc, ri->mute, ri->stereo);
197 slurm_si470x_set_volume(sc, ri->volume);
198
199 return 0;
200 }
201
202 static int
203 slurm_search(void *v, int f)
204 {
205 struct slurm_softc * const sc = v;
206
207 return slurm_si470x_search(sc, f);
208 }
209
210 static usbd_status
211 slurm_getreg(struct slurm_softc *sc, int reg, uint16_t *val)
212 {
213 usbd_status status;
214 uint8_t s[3];
215
216 ++reg;
217
218 s[0] = reg;
219 s[1] = s[2] = 0;
220
221 status = usbd_get_report(sc->sc_uif, UHID_FEATURE_REPORT,
222 reg, &s, sizeof(s));
223
224 *val = (s[1] << 8) | s[2];
225
226 return status;
227 }
228
229 static usbd_status
230 slurm_setreg(struct slurm_softc *sc, int reg, uint16_t val)
231 {
232 usbd_status status;
233 uint8_t s[3];
234
235 ++reg;
236
237 s[0] = reg;
238 s[1] = (val >> 8) & 0xff;
239 s[2] = (val >> 0) & 0xff;
240
241 status = usbd_set_report(sc->sc_uif, UHID_FEATURE_REPORT,
242 reg, &s, sizeof(s));
243
244 return status;
245 }
246
247 static int
248 slurm_si470x_await_stc(struct slurm_softc *sc)
249 {
250 int i;
251 uint16_t statusrssi;
252
253 for (i = 50; i > 0; i--) {
254 usbd_delay_ms(sc->sc_udev, 2);
255 slurm_getreg(sc, SI470X_STATUSRSSI, &statusrssi);
256 if ((statusrssi & (SI470X_STC|SI470X_SF_BL)) != 0)
257 break;
258 }
259
260 if (i == 0)
261 return -1;
262 else
263 return 0;
264 }
265
266 static void
267 slurm_si470x_get_bandspace(struct slurm_softc *sc, uint16_t sysconfig2)
268 {
269 switch (__SHIFTOUT(sysconfig2, SI470X_SPACE)) {
270 default:
271 case 0:
272 sc->sc_space = 200;
273 break;
274 case 1:
275 sc->sc_space = 100;
276 break;
277 case 2:
278 sc->sc_space = 50;
279 break;
280 }
281
282 switch (__SHIFTOUT(sysconfig2, SI470X_BAND)) {
283 default:
284 case 0:
285 sc->sc_band = 87500;
286 break;
287 case 1:
288 case 2:
289 sc->sc_band = 76000;
290 break;
291 }
292 }
293
294 static uint32_t
295 slurm_si470x_get_freq(struct slurm_softc *sc, uint16_t readchannel)
296 {
297 readchannel = __SHIFTOUT(readchannel, SI470X_READCHAN);
298 return sc->sc_band + readchannel * sc->sc_space;
299 }
300
301 static int
302 slurm_si470x_get_info(uint16_t statusrssi)
303 {
304 return (__SHIFTOUT(statusrssi, SI470X_ST) ? RADIO_INFO_STEREO : 0)
305 | (__SHIFTOUT(statusrssi, SI470X_AFCRL) ? 0 : RADIO_INFO_SIGNAL);
306 }
307
308 static int
309 slurm_si470x_get_mute(uint16_t powercfg)
310 {
311 return __SHIFTOUT(powercfg, SI470X_DMUTE) ? 0 : 1;
312 }
313
314 static int
315 slurm_si470x_get_stereo(uint16_t powercfg)
316 {
317 return __SHIFTOUT(powercfg, SI470X_MONO) ? 0 : 1;
318 }
319
320 static int
321 slurm_si470x_get_volume(uint16_t sysconfig2)
322 {
323 return __SHIFTOUT(sysconfig2, SI470X_VOLUME) * SI470X_VOLFACT;
324 }
325
326 static int
327 slurm_si470x_search(struct slurm_softc *sc, int up)
328 {
329 uint16_t powercfg;
330
331 slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
332 powercfg &= ~(SI470X_SKMODE|SI470X_SEEKUP|SI470X_SEEK);
333 powercfg |= up ? SI470X_SEEKUP : 0;
334 slurm_setreg(sc, SI470X_POWERCFG, SI470X_SEEK|powercfg);
335 slurm_si470x_await_stc(sc);
336 slurm_setreg(sc, SI470X_POWERCFG, powercfg);
337
338 return 0;
339 }
340
341 static void
342 slurm_si470x_set_freq(struct slurm_softc *sc, uint32_t freq)
343 {
344 uint16_t channel;
345
346 channel = (freq - sc->sc_band) / sc->sc_space;
347
348 slurm_setreg(sc, SI470X_CHANNEL, SI470X_TUNE|channel);
349 slurm_si470x_await_stc(sc);
350 slurm_setreg(sc, SI470X_CHANNEL, channel);
351
352 #ifdef SLURM_DEBUG
353 device_printf(sc->sc_dev, "%s 0a -> %04x after %d\n", __func__, val, i);
354 #endif
355 }
356
357 static void
358 slurm_si470x_set_powercfg(struct slurm_softc *sc, int mute, int stereo)
359 {
360 uint16_t powercfg;
361
362 slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
363 powercfg &= ~(SI470X_DMUTE|SI470X_MONO);
364 powercfg |= SI470X_DSMUTE;
365 powercfg |= mute ? 0 : SI470X_DMUTE;
366 powercfg |= stereo ? 0 : SI470X_MONO;
367 slurm_setreg(sc, SI470X_POWERCFG, powercfg);
368 }
369
370 static void
371 slurm_si470x_set_volume(struct slurm_softc *sc, int volume)
372 {
373 uint16_t sysconfig2;
374
375 slurm_getreg(sc, SI470X_SYSCONFIG2, &sysconfig2);
376 sysconfig2 &= ~SI470X_VOLUME;
377 sysconfig2 |= __SHIFTIN(volume / SI470X_VOLFACT, SI470X_VOLUME);
378 slurm_setreg(sc, SI470X_SYSCONFIG2, sysconfig2);
379 }
380