2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 /* Note: This driver doesn't compile, analog support in sigrok is WIP. */
26 #include <alsa/asoundlib.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
31 #define SAMPLE_WIDTH 16
32 #define AUDIO_DEV "plughw:0,0"
34 struct sr_analog_probe {
36 uint8_t res; /* Needs to be a power of 2, FIXME */
37 uint16_t val; /* Max hardware ADC width is 16bits */
40 struct sr_analog_sample {
41 uint8_t num_probes; /* Max hardware probes is 256 */
42 struct sr_analog_probe probes[];
45 static const int hwcaps[] = {
47 SR_HWCAP_LIMIT_SAMPLES,
51 /* TODO: Which probe names/numbers to use? */
52 static const char *probe_names[NUM_PROBES + 1] = {
58 static GSList *dev_insts = NULL;
60 /* Private, per-device-instance driver context. */
63 uint64_t limit_samples;
64 snd_pcm_t *capture_handle;
65 snd_pcm_hw_params_t *hw_params;
69 static int hw_init(const char *devinfo)
71 struct sr_dev_inst *sdi;
76 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
77 sr_err("alsa: %s: ctx malloc failed", __func__);
81 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
82 sr_err("alsa: %s: sdi was NULL", __func__);
88 dev_insts = g_slist_append(dev_insts, sdi);
97 static int hw_dev_open(int dev_index)
99 struct sr_dev_inst *sdi;
103 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
107 ret = snd_pcm_open(&ctx->capture_handle, AUDIO_DEV,
108 SND_PCM_STREAM_CAPTURE, 0);
110 sr_err("alsa: can't open audio device %s (%s)", AUDIO_DEV,
115 ret = snd_pcm_hw_params_malloc(&ctx->hw_params);
117 sr_err("alsa: can't allocate hardware parameter structure (%s)",
119 return SR_ERR_MALLOC;
122 ret = snd_pcm_hw_params_any(ctx->capture_handle, ctx->hw_params);
124 sr_err("alsa: can't initialize hardware parameter structure "
125 "(%s)", snd_strerror(ret));
132 static int hw_dev_close(int dev_index)
134 struct sr_dev_inst *sdi;
137 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
138 sr_err("alsa: %s: sdi was NULL", __func__);
142 if (!(ctx = sdi->priv)) {
143 sr_err("alsa: %s: sdi->priv was NULL", __func__);
147 // TODO: Return values of snd_*?
149 snd_pcm_hw_params_free(ctx->hw_params);
150 if (ctx->capture_handle)
151 snd_pcm_close(ctx->capture_handle);
156 static int hw_cleanup(void)
158 struct sr_dev_inst *sdi;
160 if (!(sdi = sr_dev_inst_get(dev_insts, 0))) {
161 sr_err("alsa: %s: sdi was NULL", __func__);
165 sr_dev_inst_free(sdi);
170 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
172 struct sr_dev_inst *sdi;
176 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
180 switch (dev_info_id) {
184 case SR_DI_NUM_PROBES:
185 info = GINT_TO_POINTER(NUM_PROBES);
187 case SR_DI_PROBE_NAMES:
190 case SR_DI_CUR_SAMPLERATE:
191 info = &ctx->cur_rate;
193 // case SR_DI_PROBE_TYPE:
194 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
201 static int hw_dev_status_get(int dev_index)
203 /* Avoid compiler warnings. */
204 dev_index = dev_index;
209 static const int *hw_hwcap_get_all(void)
214 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
216 struct sr_dev_inst *sdi;
219 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
224 case SR_HWCAP_PROBECONFIG:
226 case SR_HWCAP_SAMPLERATE:
227 ctx->cur_rate = *(const uint64_t *)value;
229 case SR_HWCAP_LIMIT_SAMPLES:
230 ctx->limit_samples = *(const uint64_t *)value;
237 static int receive_data(int fd, int revents, void *cb_data)
239 struct sr_dev_inst *sdi = cb_data;
240 struct context *ctx = sdi->priv;
241 struct sr_datafeed_packet packet;
242 struct sr_analog_sample *sample;
243 unsigned int sample_size = sizeof(struct sr_analog_sample) +
244 (NUM_PROBES * sizeof(struct sr_analog_probe));
253 memset(inb, 0, sizeof(inb));
254 count = snd_pcm_readi(ctx->capture_handle, inb,
255 MIN(4096 / 4, ctx->limit_samples));
257 sr_err("alsa: Failed to read samples");
261 if (!(outb = g_try_malloc(sample_size * count))) {
262 sr_err("alsa: %s: outb malloc failed", __func__);
266 for (i = 0; i < count; i++) {
267 sample = (struct sr_analog_sample *)
268 (outb + (i * sample_size));
269 sample->num_probes = NUM_PROBES;
271 for (x = 0; x < NUM_PROBES; x++) {
272 sample->probes[x].val =
273 *(uint16_t *)(inb + (i * 4) + (x * 2));
274 sample->probes[x].val &= ((1 << 16) - 1);
275 sample->probes[x].res = 16;
279 packet.type = SR_DF_ANALOG;
280 packet.length = count * sample_size;
281 packet.unitsize = sample_size;
282 packet.payload = outb;
283 sr_session_send(sdi, &packet);
285 ctx->limit_samples -= count;
287 } while (ctx->limit_samples > 0);
289 packet.type = SR_DF_END;
290 sr_session_send(sdi, &packet);
295 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
297 struct sr_dev_inst *sdi;
299 struct sr_datafeed_packet packet;
300 struct sr_datafeed_header header;
305 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
309 ret = snd_pcm_hw_params_set_access(ctx->capture_handle,
310 ctx->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
312 sr_err("alsa: can't set access type (%s)", snd_strerror(ret));
316 /* FIXME: Hardcoded for 16bits */
317 ret = snd_pcm_hw_params_set_format(ctx->capture_handle,
318 ctx->hw_params, SND_PCM_FORMAT_S16_LE);
320 sr_err("alsa: can't set sample format (%s)", snd_strerror(ret));
324 ret = snd_pcm_hw_params_set_rate_near(ctx->capture_handle,
325 ctx->hw_params, (unsigned int *)&ctx->cur_rate, 0);
327 sr_err("alsa: can't set sample rate (%s)", snd_strerror(ret));
331 ret = snd_pcm_hw_params_set_channels(ctx->capture_handle,
332 ctx->hw_params, NUM_PROBES);
334 sr_err("alsa: can't set channel count (%s)", snd_strerror(ret));
338 ret = snd_pcm_hw_params(ctx->capture_handle, ctx->hw_params);
340 sr_err("alsa: can't set parameters (%s)", snd_strerror(ret));
344 ret = snd_pcm_prepare(ctx->capture_handle);
346 sr_err("alsa: can't prepare audio interface for use (%s)",
351 count = snd_pcm_poll_descriptors_count(ctx->capture_handle);
353 sr_err("alsa: Unable to obtain poll descriptors count");
357 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
358 sr_err("alsa: %s: ufds malloc failed", __func__);
359 return SR_ERR_MALLOC;
362 ret = snd_pcm_poll_descriptors(ctx->capture_handle, ufds, count);
364 sr_err("alsa: Unable to obtain poll descriptors (%s)",
370 ctx->session_dev_id = cb_data;
371 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
373 packet.type = SR_DF_HEADER;
374 packet.length = sizeof(struct sr_datafeed_header);
375 packet.payload = (unsigned char *)&header;
376 header.feed_version = 1;
377 gettimeofday(&header.starttime, NULL);
378 header.samplerate = ctx->cur_rate;
379 header.num_analog_probes = NUM_PROBES;
380 header.num_logic_probes = 0;
381 header.protocol_id = SR_PROTO_RAW;
382 sr_session_send(cb_data, &packet);
388 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
389 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
397 SR_PRIV struct sr_dev_driver alsa_driver_info = {
399 .longname = "ALSA driver",
402 .cleanup = hw_cleanup,
403 .dev_open = hw_dev_open,
404 .dev_close = hw_dev_close,
405 .dev_info_get = hw_dev_info_get,
406 .dev_status_get = hw_dev_status_get,
407 .hwcap_get_all = hw_hwcap_get_all,
408 .dev_config_set = hw_dev_config_set,
409 .dev_acquisition_start = hw_dev_acquisition_start,
410 .dev_acquisition_stop = hw_dev_acquisition_stop,