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"
30 /* Message logging helpers with driver-specific prefix string. */
31 #define DRIVER_LOG_DOMAIN "alsa: "
32 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
36 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
37 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
40 #define SAMPLE_WIDTH 16
41 #define AUDIO_DEV "plughw:0,0"
43 struct sr_analog_probe {
45 uint8_t res; /* Needs to be a power of 2, FIXME */
46 uint16_t val; /* Max hardware ADC width is 16bits */
49 struct sr_analog_sample {
50 uint8_t num_probes; /* Max hardware probes is 256 */
51 struct sr_analog_probe probes[];
54 static const int hwcaps[] = {
56 SR_HWCAP_LIMIT_SAMPLES,
60 /* TODO: Which probe names/numbers to use? */
61 static const char *probe_names[NUM_PROBES + 1] = {
67 static GSList *dev_insts = NULL;
69 /* Private, per-device-instance driver context. */
72 uint64_t limit_samples;
73 snd_pcm_t *capture_handle;
74 snd_pcm_hw_params_t *hw_params;
78 static int hw_init(struct sr_context *sr_ctx)
80 struct sr_dev_inst *sdi;
83 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
84 sr_err("%s: ctx malloc failed", __func__);
88 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
89 sr_err("%s: sdi was NULL", __func__);
95 dev_insts = g_slist_append(dev_insts, sdi);
104 static int hw_dev_open(int dev_index)
106 struct sr_dev_inst *sdi;
110 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
114 ret = snd_pcm_open(&ctx->capture_handle, AUDIO_DEV,
115 SND_PCM_STREAM_CAPTURE, 0);
117 sr_err("Can't open audio device %s (%s).", AUDIO_DEV,
122 ret = snd_pcm_hw_params_malloc(&ctx->hw_params);
124 sr_err("Can't allocate hardware parameter structure (%s).",
126 return SR_ERR_MALLOC;
129 ret = snd_pcm_hw_params_any(ctx->capture_handle, ctx->hw_params);
131 sr_err("Can't initialize hardware parameter structure (%s)",
139 static int hw_dev_close(int dev_index)
141 struct sr_dev_inst *sdi;
144 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
145 sr_err("%s: sdi was NULL", __func__);
149 if (!(ctx = sdi->priv)) {
150 sr_err("%s: sdi->priv was NULL", __func__);
154 // TODO: Return values of snd_*?
156 snd_pcm_hw_params_free(ctx->hw_params);
157 if (ctx->capture_handle)
158 snd_pcm_close(ctx->capture_handle);
163 static int hw_cleanup(void)
165 struct sr_dev_inst *sdi;
167 if (!(sdi = sr_dev_inst_get(dev_insts, 0))) {
168 sr_err("%s: sdi was NULL", __func__);
172 sr_dev_inst_free(sdi);
177 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
179 struct sr_dev_inst *sdi;
183 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
187 switch (dev_info_id) {
191 case SR_DI_NUM_PROBES:
192 info = GINT_TO_POINTER(NUM_PROBES);
194 case SR_DI_PROBE_NAMES:
197 case SR_DI_CUR_SAMPLERATE:
198 info = &ctx->cur_rate;
200 // case SR_DI_PROBE_TYPE:
201 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
208 static int hw_dev_status_get(int dev_index)
215 static const int *hw_hwcap_get_all(void)
220 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
222 struct sr_dev_inst *sdi;
225 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
230 case SR_HWCAP_PROBECONFIG:
232 case SR_HWCAP_SAMPLERATE:
233 ctx->cur_rate = *(const uint64_t *)value;
235 case SR_HWCAP_LIMIT_SAMPLES:
236 ctx->limit_samples = *(const uint64_t *)value;
243 static int receive_data(int fd, int revents, void *cb_data)
245 struct sr_dev_inst *sdi = cb_data;
246 struct context *ctx = sdi->priv;
247 struct sr_datafeed_packet packet;
248 struct sr_analog_sample *sample;
249 unsigned int sample_size = sizeof(struct sr_analog_sample) +
250 (NUM_PROBES * sizeof(struct sr_analog_probe));
259 memset(inb, 0, sizeof(inb));
260 count = snd_pcm_readi(ctx->capture_handle, inb,
261 MIN(4096 / 4, ctx->limit_samples));
263 sr_err("Failed to read samples");
267 if (!(outb = g_try_malloc(sample_size * count))) {
268 sr_err("%s: outb malloc failed", __func__);
272 for (i = 0; i < count; i++) {
273 sample = (struct sr_analog_sample *)
274 (outb + (i * sample_size));
275 sample->num_probes = NUM_PROBES;
277 for (x = 0; x < NUM_PROBES; x++) {
278 sample->probes[x].val =
279 *(uint16_t *)(inb + (i * 4) + (x * 2));
280 sample->probes[x].val &= ((1 << 16) - 1);
281 sample->probes[x].res = 16;
285 packet.type = SR_DF_ANALOG;
286 packet.length = count * sample_size;
287 packet.unitsize = sample_size;
288 packet.payload = outb;
289 sr_session_send(sdi, &packet);
291 ctx->limit_samples -= count;
293 } while (ctx->limit_samples > 0);
295 packet.type = SR_DF_END;
296 sr_session_send(sdi, &packet);
301 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
303 struct sr_dev_inst *sdi;
305 struct sr_datafeed_packet packet;
306 struct sr_datafeed_header header;
311 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
315 ret = snd_pcm_hw_params_set_access(ctx->capture_handle,
316 ctx->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
318 sr_err("Can't set access type (%s).", snd_strerror(ret));
322 /* FIXME: Hardcoded for 16bits */
323 ret = snd_pcm_hw_params_set_format(ctx->capture_handle,
324 ctx->hw_params, SND_PCM_FORMAT_S16_LE);
326 sr_err("Can't set sample format (%s).", snd_strerror(ret));
330 ret = snd_pcm_hw_params_set_rate_near(ctx->capture_handle,
331 ctx->hw_params, (unsigned int *)&ctx->cur_rate, 0);
333 sr_err("Can't set sample rate (%s).", snd_strerror(ret));
337 ret = snd_pcm_hw_params_set_channels(ctx->capture_handle,
338 ctx->hw_params, NUM_PROBES);
340 sr_err("Can't set channel count (%s).", snd_strerror(ret));
344 ret = snd_pcm_hw_params(ctx->capture_handle, ctx->hw_params);
346 sr_err("Can't set parameters (%s).", snd_strerror(ret));
350 ret = snd_pcm_prepare(ctx->capture_handle);
352 sr_err("Can't prepare audio interface for use (%s).",
357 count = snd_pcm_poll_descriptors_count(ctx->capture_handle);
359 sr_err("Unable to obtain poll descriptors count.");
363 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
364 sr_err("%s: ufds malloc failed", __func__);
365 return SR_ERR_MALLOC;
368 ret = snd_pcm_poll_descriptors(ctx->capture_handle, ufds, count);
370 sr_err("Unable to obtain poll descriptors (%s)",
376 ctx->session_dev_id = cb_data;
377 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
379 packet.type = SR_DF_HEADER;
380 packet.length = sizeof(struct sr_datafeed_header);
381 packet.payload = (unsigned char *)&header;
382 header.feed_version = 1;
383 gettimeofday(&header.starttime, NULL);
384 header.samplerate = ctx->cur_rate;
385 header.num_analog_probes = NUM_PROBES;
386 header.num_logic_probes = 0;
387 header.protocol_id = SR_PROTO_RAW;
388 sr_session_send(cb_data, &packet);
394 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
395 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
403 SR_PRIV struct sr_dev_driver alsa_driver_info = {
405 .longname = "ALSA driver",
408 .cleanup = hw_cleanup,
409 .dev_open = hw_dev_open,
410 .dev_close = hw_dev_close,
411 .dev_info_get = hw_dev_info_get,
412 .dev_status_get = hw_dev_status_get,
413 .hwcap_get_all = hw_hwcap_get_all,
414 .dev_config_set = hw_dev_config_set,
415 .dev_acquisition_start = hw_dev_acquisition_start,
416 .dev_acquisition_stop = hw_dev_acquisition_stop,