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>
28 #include "sigrok-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 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;
74 /* Avoid compiler warnings. */
77 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
78 sr_err("alsa: %s: ctx malloc failed", __func__);
82 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
83 sr_err("alsa: %s: sdi was NULL", __func__);
89 dev_insts = g_slist_append(dev_insts, sdi);
98 static int hw_dev_open(int dev_index)
100 struct sr_dev_inst *sdi;
104 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
108 err = snd_pcm_open(&ctx->capture_handle, AUDIO_DEV,
109 SND_PCM_STREAM_CAPTURE, 0);
111 sr_err("alsa: can't open audio device %s (%s)", AUDIO_DEV,
116 err = snd_pcm_hw_params_malloc(&ctx->hw_params);
118 sr_err("alsa: can't allocate hardware parameter structure (%s)",
123 err = snd_pcm_hw_params_any(ctx->capture_handle, ctx->hw_params);
125 sr_err("alsa: can't initialize hardware parameter structure "
126 "(%s)", snd_strerror(err));
133 static int hw_dev_close(int dev_index)
135 struct sr_dev_inst *sdi;
138 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
139 sr_err("alsa: %s: sdi was NULL", __func__);
143 if (!(ctx = sdi->priv)) {
144 sr_err("alsa: %s: sdi->priv was NULL", __func__);
148 // TODO: Return values of snd_*?
150 snd_pcm_hw_params_free(ctx->hw_params);
151 if (ctx->capture_handle)
152 snd_pcm_close(ctx->capture_handle);
157 static int hw_cleanup(void)
159 struct sr_dev_inst *sdi;
161 if (!(sdi = sr_dev_inst_get(dev_insts, 0))) {
162 sr_err("alsa: %s: sdi was NULL", __func__);
166 sr_dev_inst_free(sdi);
171 static void *hw_dev_info_get(int dev_index, int dev_info_id)
173 struct sr_dev_inst *sdi;
177 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
181 switch (dev_info_id) {
185 case SR_DI_NUM_PROBES:
186 info = GINT_TO_POINTER(NUM_PROBES);
188 case SR_DI_PROBE_NAMES:
191 case SR_DI_CUR_SAMPLERATE:
192 info = &ctx->cur_rate;
194 // case SR_DI_PROBE_TYPE:
195 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
202 static int hw_dev_status_get(int dev_index)
204 /* Avoid compiler warnings. */
205 dev_index = dev_index;
210 static int *hw_hwcap_get_all(void)
215 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
217 struct sr_dev_inst *sdi;
220 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
225 case SR_HWCAP_PROBECONFIG:
227 case SR_HWCAP_SAMPLERATE:
228 ctx->cur_rate = *(uint64_t *)value;
230 case SR_HWCAP_LIMIT_SAMPLES:
231 ctx->limit_samples = *(uint64_t *)value;
238 static int receive_data(int fd, int revents, void *cb_data)
240 struct sr_dev_inst *sdi = cb_data;
241 struct context *ctx = sdi->priv;
242 struct sr_datafeed_packet packet;
243 struct sr_analog_sample *sample;
244 unsigned int sample_size = sizeof(struct sr_analog_sample) +
245 (NUM_PROBES * sizeof(struct sr_analog_probe));
254 memset(inb, 0, sizeof(inb));
255 count = snd_pcm_readi(ctx->capture_handle, inb,
256 MIN(4096 / 4, ctx->limit_samples));
258 sr_err("alsa: Failed to read samples");
262 if (!(outb = g_try_malloc(sample_size * count))) {
263 sr_err("alsa: %s: outb malloc failed", __func__);
267 for (i = 0; i < count; i++) {
268 sample = (struct sr_analog_sample *)
269 (outb + (i * sample_size));
270 sample->num_probes = NUM_PROBES;
272 for (x = 0; x < NUM_PROBES; x++) {
273 sample->probes[x].val =
274 *(uint16_t *)(inb + (i * 4) + (x * 2));
275 sample->probes[x].val &= ((1 << 16) - 1);
276 sample->probes[x].res = 16;
280 packet.type = SR_DF_ANALOG;
281 packet.length = count * sample_size;
282 packet.unitsize = sample_size;
283 packet.payload = outb;
284 sr_session_send(sdi, &packet);
286 ctx->limit_samples -= count;
288 } while (ctx->limit_samples > 0);
290 packet.type = SR_DF_END;
291 sr_session_send(sdi, &packet);
296 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
298 struct sr_dev_inst *sdi;
300 struct sr_datafeed_packet packet;
301 struct sr_datafeed_header header;
306 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
310 err = snd_pcm_hw_params_set_access(ctx->capture_handle,
311 ctx->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
313 sr_err("alsa: can't set access type (%s)", snd_strerror(err));
317 /* FIXME: Hardcoded for 16bits */
318 err = snd_pcm_hw_params_set_format(ctx->capture_handle,
319 ctx->hw_params, SND_PCM_FORMAT_S16_LE);
321 sr_err("alsa: can't set sample format (%s)", snd_strerror(err));
325 err = snd_pcm_hw_params_set_rate_near(ctx->capture_handle,
326 ctx->hw_params, (unsigned int *)&ctx->cur_rate, 0);
328 sr_err("alsa: can't set sample rate (%s)", snd_strerror(err));
332 err = snd_pcm_hw_params_set_channels(ctx->capture_handle,
333 ctx->hw_params, NUM_PROBES);
335 sr_err("alsa: can't set channel count (%s)", snd_strerror(err));
339 err = snd_pcm_hw_params(ctx->capture_handle, ctx->hw_params);
341 sr_err("alsa: can't set parameters (%s)", snd_strerror(err));
345 err = snd_pcm_prepare(ctx->capture_handle);
347 sr_err("alsa: can't prepare audio interface for use (%s)",
352 count = snd_pcm_poll_descriptors_count(ctx->capture_handle);
354 sr_err("alsa: Unable to obtain poll descriptors count");
358 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
359 sr_err("alsa: %s: ufds malloc failed", __func__);
360 return SR_ERR_MALLOC;
363 err = snd_pcm_poll_descriptors(ctx->capture_handle, ufds, count);
365 sr_err("alsa: Unable to obtain poll descriptors (%s)",
371 ctx->session_dev_id = cb_data;
372 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
374 packet.type = SR_DF_HEADER;
375 packet.length = sizeof(struct sr_datafeed_header);
376 packet.payload = (unsigned char *)&header;
377 header.feed_version = 1;
378 gettimeofday(&header.starttime, NULL);
379 header.samplerate = ctx->cur_rate;
380 header.num_analog_probes = NUM_PROBES;
381 header.num_logic_probes = 0;
382 header.protocol_id = SR_PROTO_RAW;
383 sr_session_send(cb_data, &packet);
389 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
390 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
392 /* Avoid compiler warnings. */
399 SR_PRIV struct sr_dev_driver alsa_driver_info = {
401 .longname = "ALSA driver",
404 .cleanup = hw_cleanup,
405 .dev_open = hw_dev_open,
406 .dev_close = hw_dev_close,
407 .dev_info_get = hw_dev_info_get,
408 .dev_status_get = hw_dev_status_get,
409 .hwcap_get_all = hw_hwcap_get_all,
410 .dev_config_set = hw_dev_config_set,
411 .dev_acquisition_start = hw_dev_acquisition_start,
412 .dev_acquisition_stop = hw_dev_acquisition_stop,