1 /*        $NetBSD: slurm.c,v 1.4 2019/01/22 06:47:20 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.4 2019/01/22 06:47:20 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
slurm_match(device_t parent,cfdata_t match,void * aux)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
slurm_attach(device_t parent,device_t self,void * aux)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
slurm_detach(device_t self,int flags)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, sc->sc_dev);
158 
159           return rv;
160 }
161 
162 static int
slurm_get_info(void * v,struct radio_info * ri)163 slurm_get_info(void *v, struct radio_info *ri)
164 {
165           struct slurm_softc * const sc = v;
166           uint16_t powercfg, sysconfig2, readchannel, statusrssi;
167 
168           slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
169           slurm_getreg(sc, SI470X_SYSCONFIG2, &sysconfig2);
170           slurm_getreg(sc, SI470X_STATUSRSSI, &statusrssi);
171           slurm_getreg(sc, SI470X_READCHANNEL, &readchannel);
172 
173           ri->mute = slurm_si470x_get_mute(powercfg);
174           ri->volume = slurm_si470x_get_volume(sysconfig2);
175           ri->stereo = slurm_si470x_get_stereo(powercfg);
176           ri->rfreq = 0;
177           ri->lock = 0;
178           slurm_si470x_get_bandspace(sc, sysconfig2);
179           ri->freq = slurm_si470x_get_freq(sc, readchannel);
180           ri->caps = RADIO_CAPS_DETECT_STEREO | RADIO_CAPS_DETECT_SIGNAL |
181                        RADIO_CAPS_SET_MONO | RADIO_CAPS_HW_SEARCH |
182                        RADIO_CAPS_HW_AFC | RADIO_CAPS_LOCK_SENSITIVITY;
183           ri->info = slurm_si470x_get_info(statusrssi);
184 
185           return 0;
186 }
187 
188 static int
slurm_set_info(void * v,struct radio_info * ri)189 slurm_set_info(void *v, struct radio_info *ri)
190 {
191           struct slurm_softc * const sc = v;
192 
193           slurm_si470x_set_freq(sc, ri->freq);
194           slurm_si470x_set_powercfg(sc, ri->mute, ri->stereo);
195           slurm_si470x_set_volume(sc, ri->volume);
196 
197           return 0;
198 }
199 
200 static int
slurm_search(void * v,int f)201 slurm_search(void *v, int f)
202 {
203           struct slurm_softc * const sc = v;
204 
205           return slurm_si470x_search(sc, f);
206 }
207 
208 static usbd_status
slurm_getreg(struct slurm_softc * sc,int reg,uint16_t * val)209 slurm_getreg(struct slurm_softc *sc, int reg, uint16_t *val)
210 {
211           usbd_status status;
212           uint8_t s[3];
213 
214           ++reg;
215 
216           s[0] = reg;
217           s[1] = s[2] = 0;
218 
219           status = usbd_get_report(sc->sc_uif, UHID_FEATURE_REPORT,
220                     reg, &s, sizeof(s));
221 
222           *val = (s[1] << 8) | s[2];
223 
224           return status;
225 }
226 
227 static usbd_status
slurm_setreg(struct slurm_softc * sc,int reg,uint16_t val)228 slurm_setreg(struct slurm_softc *sc, int reg, uint16_t val)
229 {
230           usbd_status status;
231           uint8_t s[3];
232 
233           ++reg;
234 
235           s[0] = reg;
236           s[1] = (val >> 8) & 0xff;
237           s[2] = (val >> 0) & 0xff;
238 
239           status = usbd_set_report(sc->sc_uif, UHID_FEATURE_REPORT,
240                     reg, &s, sizeof(s));
241 
242           return status;
243 }
244 
245 static int
slurm_si470x_await_stc(struct slurm_softc * sc)246 slurm_si470x_await_stc(struct slurm_softc *sc)
247 {
248           int i;
249           uint16_t statusrssi;
250 
251           for (i = 50; i > 0; i--) {
252                     usbd_delay_ms(sc->sc_udev, 2);
253                     slurm_getreg(sc, SI470X_STATUSRSSI, &statusrssi);
254                     if ((statusrssi & (SI470X_STC|SI470X_SF_BL)) != 0)
255                               break;
256           }
257 
258           if (i == 0)
259                     return -1;
260           else
261                     return 0;
262 }
263 
264 static void
slurm_si470x_get_bandspace(struct slurm_softc * sc,uint16_t sysconfig2)265 slurm_si470x_get_bandspace(struct slurm_softc *sc, uint16_t sysconfig2)
266 {
267           switch (__SHIFTOUT(sysconfig2, SI470X_SPACE)) {
268           default:
269           case 0:
270                     sc->sc_space = 200;
271                     break;
272           case 1:
273                     sc->sc_space = 100;
274                     break;
275           case 2:
276                     sc->sc_space = 50;
277                     break;
278           }
279 
280           switch (__SHIFTOUT(sysconfig2, SI470X_BAND)) {
281           default:
282           case 0:
283                     sc->sc_band = 87500;
284                     break;
285           case 1:
286           case 2:
287                     sc->sc_band = 76000;
288                     break;
289           }
290 }
291 
292 static uint32_t
slurm_si470x_get_freq(struct slurm_softc * sc,uint16_t readchannel)293 slurm_si470x_get_freq(struct slurm_softc *sc, uint16_t readchannel)
294 {
295           readchannel = __SHIFTOUT(readchannel, SI470X_READCHAN);
296           return sc->sc_band + readchannel * sc->sc_space;
297 }
298 
299 static int
slurm_si470x_get_info(uint16_t statusrssi)300 slurm_si470x_get_info(uint16_t statusrssi)
301 {
302           return (__SHIFTOUT(statusrssi, SI470X_ST) ? RADIO_INFO_STEREO : 0)
303               | (__SHIFTOUT(statusrssi, SI470X_AFCRL) ? 0 : RADIO_INFO_SIGNAL);
304 }
305 
306 static int
slurm_si470x_get_mute(uint16_t powercfg)307 slurm_si470x_get_mute(uint16_t powercfg)
308 {
309           return __SHIFTOUT(powercfg, SI470X_DMUTE) ? 0 : 1;
310 }
311 
312 static int
slurm_si470x_get_stereo(uint16_t powercfg)313 slurm_si470x_get_stereo(uint16_t powercfg)
314 {
315           return __SHIFTOUT(powercfg, SI470X_MONO) ? 0 : 1;
316 }
317 
318 static int
slurm_si470x_get_volume(uint16_t sysconfig2)319 slurm_si470x_get_volume(uint16_t sysconfig2)
320 {
321           return __SHIFTOUT(sysconfig2, SI470X_VOLUME) * SI470X_VOLFACT;
322 }
323 
324 static int
slurm_si470x_search(struct slurm_softc * sc,int up)325 slurm_si470x_search(struct slurm_softc *sc, int up)
326 {
327           uint16_t powercfg;
328 
329           slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
330           powercfg &= ~(SI470X_SKMODE|SI470X_SEEKUP|SI470X_SEEK);
331           powercfg |= up ? SI470X_SEEKUP : 0;
332           slurm_setreg(sc, SI470X_POWERCFG, SI470X_SEEK|powercfg);
333           slurm_si470x_await_stc(sc);
334           slurm_setreg(sc, SI470X_POWERCFG, powercfg);
335 
336           return 0;
337 }
338 
339 static void
slurm_si470x_set_freq(struct slurm_softc * sc,uint32_t freq)340 slurm_si470x_set_freq(struct slurm_softc *sc, uint32_t freq)
341 {
342           uint16_t channel;
343 
344           channel = (freq - sc->sc_band) / sc->sc_space;
345 
346           slurm_setreg(sc, SI470X_CHANNEL, SI470X_TUNE|channel);
347           slurm_si470x_await_stc(sc);
348           slurm_setreg(sc, SI470X_CHANNEL, channel);
349 
350 #ifdef SLURM_DEBUG
351           device_printf(sc->sc_dev, "%s 0a -> %04x after %d\n", __func__, val, i);
352 #endif
353 }
354 
355 static void
slurm_si470x_set_powercfg(struct slurm_softc * sc,int mute,int stereo)356 slurm_si470x_set_powercfg(struct slurm_softc *sc, int mute, int stereo)
357 {
358           uint16_t powercfg;
359 
360           slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
361           powercfg &= ~(SI470X_DMUTE|SI470X_MONO);
362           powercfg |= SI470X_DSMUTE;
363           powercfg |= mute ? 0 : SI470X_DMUTE;
364           powercfg |= stereo ? 0 : SI470X_MONO;
365           slurm_setreg(sc, SI470X_POWERCFG, powercfg);
366 }
367 
368 static void
slurm_si470x_set_volume(struct slurm_softc * sc,int volume)369 slurm_si470x_set_volume(struct slurm_softc *sc, int volume)
370 {
371           uint16_t sysconfig2;
372 
373           slurm_getreg(sc, SI470X_SYSCONFIG2, &sysconfig2);
374           sysconfig2 &= ~SI470X_VOLUME;
375           sysconfig2 |= __SHIFTIN(volume / SI470X_VOLFACT, SI470X_VOLUME);
376           slurm_setreg(sc, SI470X_SYSCONFIG2, sysconfig2);
377 }
378