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 static const char *probe_names[NUM_PROBES + 1] = {
57 static GSList *dev_insts = NULL;
61 uint64_t limit_samples;
62 snd_pcm_t *capture_handle;
63 snd_pcm_hw_params_t *hw_params;
67 static int hw_init(const char *devinfo)
69 struct sr_dev_inst *sdi;
72 /* Avoid compiler warnings. */
75 if (!(alsa = g_try_malloc0(sizeof(struct alsa)))) {
76 sr_err("alsa: %s: alsa malloc failed", __func__);
80 sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL);
86 dev_insts = g_slist_append(dev_insts, sdi);
95 static int hw_dev_open(int dev_index)
97 struct sr_dev_inst *sdi;
101 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
105 err = snd_pcm_open(&alsa->capture_handle, AUDIO_DEV,
106 SND_PCM_STREAM_CAPTURE, 0);
108 sr_err("alsa: can't open audio device %s (%s)", AUDIO_DEV,
113 err = snd_pcm_hw_params_malloc(&alsa->hw_params);
115 sr_err("alsa: can't allocate hardware parameter structure (%s)",
120 err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params);
122 sr_err("alsa: can't initialize hardware parameter structure "
123 "(%s)", snd_strerror(err));
130 static int hw_dev_close(int dev_index)
132 struct sr_dev_inst *sdi;
135 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
136 sr_err("alsa: %s: sdi was NULL", __func__);
137 return SR_ERR; /* TODO: SR_ERR_ARG? */
140 if (!(alsa = sdi->priv)) {
141 sr_err("alsa: %s: sdi->priv was NULL", __func__);
142 return SR_ERR; /* TODO: SR_ERR_ARG? */
145 // TODO: Return values of snd_*?
147 snd_pcm_hw_params_free(alsa->hw_params);
148 if (alsa->capture_handle)
149 snd_pcm_close(alsa->capture_handle);
154 static int hw_cleanup(void)
156 struct sr_dev_inst *sdi;
158 if (!(sdi = sr_dev_inst_get(dev_insts, 0))) {
159 sr_err("alsa: %s: sdi was NULL", __func__);
163 sr_dev_inst_free(sdi);
168 static void *hw_dev_info_get(int dev_index, int dev_info_id)
170 struct sr_dev_inst *sdi;
174 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
178 switch (dev_info_id) {
182 case SR_DI_NUM_PROBES:
183 info = GINT_TO_POINTER(NUM_PROBES);
185 case SR_DI_PROBE_NAMES:
188 case SR_DI_CUR_SAMPLERATE:
189 info = &alsa->cur_rate;
191 // case SR_DI_PROBE_TYPE:
192 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
199 static int hw_dev_status_get(int dev_index)
201 /* Avoid compiler warnings. */
202 dev_index = dev_index;
207 static int *hw_hwcap_get_all(void)
212 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
214 struct sr_dev_inst *sdi;
217 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
222 case SR_HWCAP_PROBECONFIG:
224 case SR_HWCAP_SAMPLERATE:
225 alsa->cur_rate = *(uint64_t *) value;
227 case SR_HWCAP_LIMIT_SAMPLES:
228 alsa->limit_samples = *(uint64_t *) value;
235 static int receive_data(int fd, int revents, void *user_data)
237 struct sr_dev_inst *sdi = user_data;
238 struct alsa *alsa = sdi->priv;
239 struct sr_datafeed_packet packet;
240 struct sr_analog_sample *sample;
241 unsigned int sample_size = sizeof(struct sr_analog_sample) +
242 (NUM_PROBES * sizeof(struct sr_analog_probe));
251 memset(inb, 0, sizeof(inb));
252 count = snd_pcm_readi(alsa->capture_handle, inb,
253 MIN(4096/4, alsa->limit_samples));
255 sr_err("alsa: Failed to read samples");
259 if (!(outb = g_try_malloc(sample_size * count))) {
260 sr_err("alsa: %s: outb malloc failed", __func__);
264 for (i = 0; i < count; i++) {
265 sample = (struct sr_analog_sample *)
266 (outb + (i * sample_size));
267 sample->num_probes = NUM_PROBES;
269 for (x = 0; x < NUM_PROBES; x++) {
270 sample->probes[x].val =
271 *(uint16_t *) (inb + (i * 4) + (x * 2));
272 sample->probes[x].val &= ((1 << 16) - 1);
273 sample->probes[x].res = 16;
277 packet.type = SR_DF_ANALOG;
278 packet.length = count * sample_size;
279 packet.unitsize = sample_size;
280 packet.payload = outb;
281 sr_session_bus(user_data, &packet);
283 alsa->limit_samples -= count;
285 } while (alsa->limit_samples > 0);
287 packet.type = SR_DF_END;
288 sr_session_bus(user_data, &packet);
293 static int hw_dev_acquisition_start(int dev_index, gpointer session_dev_id)
295 struct sr_dev_inst *sdi;
297 struct sr_datafeed_packet packet;
298 struct sr_datafeed_header header;
303 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
307 err = snd_pcm_hw_params_set_access(alsa->capture_handle,
308 alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
310 sr_err("alsa: can't set access type (%s)", snd_strerror(err));
314 /* FIXME: Hardcoded for 16bits */
315 err = snd_pcm_hw_params_set_format(alsa->capture_handle,
316 alsa->hw_params, SND_PCM_FORMAT_S16_LE);
318 sr_err("alsa: can't set sample format (%s)", snd_strerror(err));
322 err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle,
323 alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0);
325 sr_err("alsa: can't set sample rate (%s)", snd_strerror(err));
329 err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
330 alsa->hw_params, NUM_PROBES);
332 sr_err("alsa: can't set channel count (%s)", snd_strerror(err));
336 err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
338 sr_err("alsa: can't set parameters (%s)", snd_strerror(err));
342 err = snd_pcm_prepare(alsa->capture_handle);
344 sr_err("alsa: can't prepare audio interface for use (%s)",
349 count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
351 sr_err("alsa: Unable to obtain poll descriptors count");
355 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
356 sr_err("alsa: %s: ufds malloc failed", __func__);
357 return SR_ERR_MALLOC;
360 err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
362 sr_err("alsa: Unable to obtain poll descriptors (%s)",
368 alsa->session_id = session_dev_id;
369 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
371 packet.type = SR_DF_HEADER;
372 packet.length = sizeof(struct sr_datafeed_header);
373 packet.payload = (unsigned char *) &header;
374 header.feed_version = 1;
375 gettimeofday(&header.starttime, NULL);
376 header.samplerate = alsa->cur_rate;
377 header.num_analog_probes = NUM_PROBES;
378 header.num_logic_probes = 0;
379 header.protocol_id = SR_PROTO_RAW;
380 sr_session_bus(session_dev_id, &packet);
386 static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id)
388 /* Avoid compiler warnings. */
389 dev_index = dev_index;
390 session_dev_id = session_dev_id;
395 SR_PRIV struct sr_dev_plugin alsa_plugin_info = {
397 .longname = "ALSA driver",
400 .cleanup = hw_cleanup,
401 .dev_open = hw_dev_open,
402 .dev_close = hw_dev_close,
403 .dev_info_get = hw_dev_info_get,
404 .dev_status_get = hw_dev_status_get,
405 .hwcap_get_all = hw_hwcap_get_all,
406 .dev_config_set = hw_dev_config_set,
407 .dev_acquisition_start = hw_dev_acquisition_start,
408 .dev_acquisition_stop = hw_dev_acquisition_stop,