summaryrefslogtreecommitdiff
path: root/sys/dev/usb/slurm.c
blob: ab006d0ae904a84b5e6421eeda3847208ac961cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*	$NetBSD: slurm.c,v 1.4 2019/01/22 06:47:20 skrll Exp $ */

/*
 * Copyright (c) 2012 Jonathan A. Kollasch
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: slurm.c,v 1.4 2019/01/22 06:47:20 skrll Exp $");

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/ic/si470x_reg.h>

#include <sys/radioio.h>
#include <dev/radio_if.h>

#ifdef SLURM_DEBUG
int	slurmdebug = 0;
#define DPRINTFN(n, x)	do { if (slurmdebug > (n)) printf x; } while (0)
#else
#define DPRINTFN(n, x)
#endif

#define DPRINTF(x) DPRINTFN(0, x)

#define SI470X_VOLFACT (255 / __SHIFTOUT_MASK(SI470X_VOLUME))

struct slurm_softc {
	device_t		sc_dev;
	struct usbd_device *	sc_udev;
	struct usbd_interface *	sc_uif;
	uint32_t		sc_band;
	uint32_t		sc_space;
};

static const struct usb_devno slurm_devs[] = {
	{ USB_VENDOR_ADS, USB_PRODUCT_ADS_RDX155 },
};

static int slurm_match(device_t, cfdata_t, void *);
static void slurm_attach(device_t, device_t, void *);
static int slurm_detach(device_t, int);

static int slurm_get_info(void *, struct radio_info *);
static int slurm_set_info(void *, struct radio_info *);
static int slurm_search(void *, int);

static usbd_status slurm_setreg(struct slurm_softc *, int, uint16_t);
static usbd_status slurm_getreg(struct slurm_softc *, int, uint16_t *);

static uint32_t slurm_si470x_get_freq(struct slurm_softc *, uint16_t);
static void slurm_si470x_get_bandspace(struct slurm_softc *, uint16_t);
static int slurm_si470x_get_info(uint16_t);
static int slurm_si470x_get_mute(uint16_t);
static int slurm_si470x_get_stereo(uint16_t);
static int slurm_si470x_get_volume(uint16_t);

static int slurm_si470x_search(struct slurm_softc *, int);

static void slurm_si470x_set_freq(struct slurm_softc *, uint32_t);
static void slurm_si470x_set_powercfg(struct slurm_softc *, int, int);
static void slurm_si470x_set_volume(struct slurm_softc *, int);

static const struct radio_hw_if slurm_radio = {
	.get_info = slurm_get_info,
	.set_info = slurm_set_info,
	.search = slurm_search,
};

CFATTACH_DECL_NEW(slurm, sizeof(struct slurm_softc),
    slurm_match, slurm_attach, slurm_detach, NULL);

static int
slurm_match(device_t parent, cfdata_t match, void *aux)
{
	const struct usbif_attach_arg * const uiaa = aux;

	if (uiaa->uiaa_ifaceno != 2)
		return UMATCH_NONE;

	if (usb_lookup(slurm_devs, uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL) {
		return UMATCH_VENDOR_PRODUCT;
	}

	return UMATCH_NONE;
}

static void
slurm_attach(device_t parent, device_t self, void *aux)
{
	struct slurm_softc * const sc = device_private(self);
	const struct usbif_attach_arg * const uiaa = aux;

	sc->sc_dev = self;
	sc->sc_udev = uiaa->uiaa_device;
	sc->sc_uif = uiaa->uiaa_iface;

	aprint_normal("\n");
	aprint_naive("\n");

	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);

#ifdef SLURM_DEBUG
	{
		uint16_t val;
		for (int i = 0; i < 16; i++) {
			slurm_getreg(sc, i, &val);
			device_printf(self, "%02x -> %04x\n", i, val);
		}
	}
#endif

	radio_attach_mi(&slurm_radio, sc, self);
}

static int
slurm_detach(device_t self, int flags)
{
	struct slurm_softc * const sc = device_private(self);
	int rv = 0;

	if ((rv = config_detach_children(self, flags)) != 0)
		return rv;

	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);

	return rv;
}

static int
slurm_get_info(void *v, struct radio_info *ri)
{
	struct slurm_softc * const sc = v;
	uint16_t powercfg, sysconfig2, readchannel, statusrssi;

	slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
	slurm_getreg(sc, SI470X_SYSCONFIG2, &sysconfig2);
	slurm_getreg(sc, SI470X_STATUSRSSI, &statusrssi);
	slurm_getreg(sc, SI470X_READCHANNEL, &readchannel);

	ri->mute = slurm_si470x_get_mute(powercfg);
	ri->volume = slurm_si470x_get_volume(sysconfig2);
	ri->stereo = slurm_si470x_get_stereo(powercfg);
	ri->rfreq = 0;
	ri->lock = 0;
	slurm_si470x_get_bandspace(sc, sysconfig2);
	ri->freq = slurm_si470x_get_freq(sc, readchannel);
	ri->caps = RADIO_CAPS_DETECT_STEREO | RADIO_CAPS_DETECT_SIGNAL |
		   RADIO_CAPS_SET_MONO | RADIO_CAPS_HW_SEARCH |
		   RADIO_CAPS_HW_AFC | RADIO_CAPS_LOCK_SENSITIVITY;
	ri->info = slurm_si470x_get_info(statusrssi);

	return 0;
}

static int
slurm_set_info(void *v, struct radio_info *ri)
{
	struct slurm_softc * const sc = v;

	slurm_si470x_set_freq(sc, ri->freq);
	slurm_si470x_set_powercfg(sc, ri->mute, ri->stereo);
	slurm_si470x_set_volume(sc, ri->volume);

	return 0;
}

static int
slurm_search(void *v, int f)
{
	struct slurm_softc * const sc = v;

	return slurm_si470x_search(sc, f);
}

static usbd_status
slurm_getreg(struct slurm_softc *sc, int reg, uint16_t *val)
{
	usbd_status status;
	uint8_t s[3];

	++reg;

	s[0] = reg;
	s[1] = s[2] = 0;

	status = usbd_get_report(sc->sc_uif, UHID_FEATURE_REPORT,
		reg, &s, sizeof(s));

	*val = (s[1] << 8) | s[2];

	return status;
}

static usbd_status
slurm_setreg(struct slurm_softc *sc, int reg, uint16_t val)
{
	usbd_status status;
	uint8_t s[3];

	++reg;

	s[0] = reg;
	s[1] = (val >> 8) & 0xff;
	s[2] = (val >> 0) & 0xff;

	status = usbd_set_report(sc->sc_uif, UHID_FEATURE_REPORT,
		reg, &s, sizeof(s));

	return status;
}

static int
slurm_si470x_await_stc(struct slurm_softc *sc)
{
	int i;
	uint16_t statusrssi;

	for (i = 50; i > 0; i--) {
		usbd_delay_ms(sc->sc_udev, 2);
		slurm_getreg(sc, SI470X_STATUSRSSI, &statusrssi);
		if ((statusrssi & (SI470X_STC|SI470X_SF_BL)) != 0)
			break;
	}

	if (i == 0)
		return -1;
	else
		return 0;
}

static void
slurm_si470x_get_bandspace(struct slurm_softc *sc, uint16_t sysconfig2)
{
	switch (__SHIFTOUT(sysconfig2, SI470X_SPACE)) {
	default:
	case 0:
		sc->sc_space = 200;
		break;
	case 1:
		sc->sc_space = 100;
		break;
	case 2:
		sc->sc_space = 50;
		break;
	}

	switch (__SHIFTOUT(sysconfig2, SI470X_BAND)) {
	default:
	case 0:
		sc->sc_band = 87500;
		break;
	case 1:
	case 2:
		sc->sc_band = 76000;
		break;
	}
}

static uint32_t
slurm_si470x_get_freq(struct slurm_softc *sc, uint16_t readchannel)
{
	readchannel = __SHIFTOUT(readchannel, SI470X_READCHAN);
	return sc->sc_band + readchannel * sc->sc_space;
}

static int
slurm_si470x_get_info(uint16_t statusrssi)
{
	return (__SHIFTOUT(statusrssi, SI470X_ST) ? RADIO_INFO_STEREO : 0)
	    | (__SHIFTOUT(statusrssi, SI470X_AFCRL) ? 0 : RADIO_INFO_SIGNAL);
}

static int
slurm_si470x_get_mute(uint16_t powercfg)
{
	return __SHIFTOUT(powercfg, SI470X_DMUTE) ? 0 : 1;
}

static int
slurm_si470x_get_stereo(uint16_t powercfg)
{
	return __SHIFTOUT(powercfg, SI470X_MONO) ? 0 : 1;
}

static int
slurm_si470x_get_volume(uint16_t sysconfig2)
{
	return __SHIFTOUT(sysconfig2, SI470X_VOLUME) * SI470X_VOLFACT;
}

static int
slurm_si470x_search(struct slurm_softc *sc, int up)
{
	uint16_t powercfg;

	slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
	powercfg &= ~(SI470X_SKMODE|SI470X_SEEKUP|SI470X_SEEK);
	powercfg |= up ? SI470X_SEEKUP : 0;
	slurm_setreg(sc, SI470X_POWERCFG, SI470X_SEEK|powercfg);
	slurm_si470x_await_stc(sc);
	slurm_setreg(sc, SI470X_POWERCFG, powercfg);

	return 0;
}

static void
slurm_si470x_set_freq(struct slurm_softc *sc, uint32_t freq)
{
	uint16_t channel;

	channel = (freq - sc->sc_band) / sc->sc_space;

	slurm_setreg(sc, SI470X_CHANNEL, SI470X_TUNE|channel);
	slurm_si470x_await_stc(sc);
	slurm_setreg(sc, SI470X_CHANNEL, channel);

#ifdef SLURM_DEBUG
	device_printf(sc->sc_dev, "%s 0a -> %04x after %d\n", __func__, val, i);
#endif
}

static void
slurm_si470x_set_powercfg(struct slurm_softc *sc, int mute, int stereo)
{
	uint16_t powercfg;

	slurm_getreg(sc, SI470X_POWERCFG, &powercfg);
	powercfg &= ~(SI470X_DMUTE|SI470X_MONO);
	powercfg |= SI470X_DSMUTE;
	powercfg |= mute ? 0 : SI470X_DMUTE;
	powercfg |= stereo ? 0 : SI470X_MONO;
	slurm_setreg(sc, SI470X_POWERCFG, powercfg);
}

static void
slurm_si470x_set_volume(struct slurm_softc *sc, int volume)
{
	uint16_t sysconfig2;

	slurm_getreg(sc, SI470X_SYSCONFIG2, &sysconfig2);
	sysconfig2 &= ~SI470X_VOLUME;
	sysconfig2 |= __SHIFTIN(volume / SI470X_VOLFACT, SI470X_VOLUME);
	slurm_setreg(sc, SI470X_SYSCONFIG2, sysconfig2);
}