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 capabilities[] = {
47 SR_HWCAP_LIMIT_SAMPLES,
51 static const char *probe_names[NUM_PROBES + 1] = {
57 static GSList *device_instances = 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 *deviceinfo)
69 struct sr_device_instance *sdi;
72 /* Avoid compiler warnings. */
73 deviceinfo = deviceinfo;
75 if (!(alsa = g_try_malloc0(sizeof(struct alsa)))) {
76 sr_err("alsa: %s: alsa malloc failed", __func__);
80 sdi = sr_device_instance_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL);
86 device_instances = g_slist_append(device_instances, sdi);
94 static int hw_opendev(int device_index)
96 struct sr_device_instance *sdi;
100 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
104 err = snd_pcm_open(&alsa->capture_handle, AUDIO_DEV,
105 SND_PCM_STREAM_CAPTURE, 0);
107 sr_warn("cannot open audio device %s (%s)", AUDIO_DEV,
112 err = snd_pcm_hw_params_malloc(&alsa->hw_params);
114 sr_warn("cannot allocate hardware parameter structure (%s)",
119 err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params);
121 sr_warn("cannot initialize hardware parameter structure (%s)",
129 static int hw_closedev(int device_index)
131 struct sr_device_instance *sdi;
134 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
135 sr_err("alsa: %s: sdi was NULL", __func__);
136 return SR_ERR; /* TODO: SR_ERR_ARG? */
139 if (!(alsa = sdi->priv)) {
140 sr_err("alsa: %s: sdi->priv was NULL", __func__);
141 return SR_ERR; /* TODO: SR_ERR_ARG? */
144 // TODO: Return values of snd_*?
146 snd_pcm_hw_params_free(alsa->hw_params);
147 if (alsa->capture_handle)
148 snd_pcm_close(alsa->capture_handle);
153 static void hw_cleanup(void)
155 struct sr_device_instance *sdi;
157 if (!(sdi = sr_get_device_instance(device_instances, 0)))
161 sr_device_instance_free(sdi);
164 static void *hw_get_device_info(int device_index, int device_info_id)
166 struct sr_device_instance *sdi;
170 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
174 switch (device_info_id) {
178 case SR_DI_NUM_PROBES:
179 info = GINT_TO_POINTER(NUM_PROBES);
181 case SR_DI_PROBE_NAMES:
184 case SR_DI_CUR_SAMPLERATE:
185 info = &alsa->cur_rate;
187 // case SR_DI_PROBE_TYPE:
188 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
195 static int hw_get_status(int device_index)
197 /* Avoid compiler warnings. */
198 device_index = device_index;
203 static int *hw_get_capabilities(void)
208 static int hw_set_configuration(int device_index, int capability, void *value)
210 struct sr_device_instance *sdi;
213 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
217 switch (capability) {
218 case SR_HWCAP_PROBECONFIG:
220 case SR_HWCAP_SAMPLERATE:
221 alsa->cur_rate = *(uint64_t *) value;
223 case SR_HWCAP_LIMIT_SAMPLES:
224 alsa->limit_samples = *(uint64_t *) value;
231 static int receive_data(int fd, int revents, void *user_data)
233 struct sr_device_instance *sdi = user_data;
234 struct alsa *alsa = sdi->priv;
235 struct sr_datafeed_packet packet;
236 struct sr_analog_sample *sample;
237 unsigned int sample_size = sizeof(struct sr_analog_sample) +
238 (NUM_PROBES * sizeof(struct sr_analog_probe));
247 memset(inb, 0, sizeof(inb));
248 count = snd_pcm_readi(alsa->capture_handle, inb,
249 MIN(4096/4, alsa->limit_samples));
251 sr_warn("Failed to read samples");
255 if (!(outb = g_try_malloc(sample_size * count))) {
256 sr_err("alsa: %s: outb malloc failed", __func__);
260 for (i = 0; i < count; i++) {
261 sample = (struct sr_analog_sample *)
262 (outb + (i * sample_size));
263 sample->num_probes = NUM_PROBES;
265 for (x = 0; x < NUM_PROBES; x++) {
266 sample->probes[x].val =
267 *(uint16_t *) (inb + (i * 4) + (x * 2));
268 sample->probes[x].val &= ((1 << 16) - 1);
269 sample->probes[x].res = 16;
273 packet.type = SR_DF_ANALOG;
274 packet.length = count * sample_size;
275 packet.unitsize = sample_size;
276 packet.payload = outb;
277 sr_session_bus(user_data, &packet);
279 alsa->limit_samples -= count;
281 } while (alsa->limit_samples > 0);
283 packet.type = SR_DF_END;
284 sr_session_bus(user_data, &packet);
289 static int hw_start_acquisition(int device_index, gpointer session_device_id)
291 struct sr_device_instance *sdi;
293 struct sr_datafeed_packet packet;
294 struct sr_datafeed_header header;
299 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
303 err = snd_pcm_hw_params_set_access(alsa->capture_handle,
304 alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
306 sr_warn("cannot set access type (%s)", snd_strerror(err));
310 /* FIXME: Hardcoded for 16bits */
311 err = snd_pcm_hw_params_set_format(alsa->capture_handle,
312 alsa->hw_params, SND_PCM_FORMAT_S16_LE);
314 sr_warn("cannot set sample format (%s)", snd_strerror(err));
318 err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle,
319 alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0);
321 sr_warn("cannot set sample rate (%s)", snd_strerror(err));
325 err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
326 alsa->hw_params, NUM_PROBES);
328 sr_warn("cannot set channel count (%s)", snd_strerror(err));
332 err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
334 sr_warn("cannot set parameters (%s)", snd_strerror(err));
338 err = snd_pcm_prepare(alsa->capture_handle);
340 sr_warn("cannot prepare audio interface for use (%s)",
345 count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
347 sr_warn("Unable to obtain poll descriptors count");
351 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
352 sr_warn("alsa: %s: ufds malloc failed", __func__);
353 return SR_ERR_MALLOC;
356 err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
358 sr_warn("Unable to obtain poll descriptors (%s)",
364 alsa->session_id = session_device_id;
365 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
367 packet.type = SR_DF_HEADER;
368 packet.length = sizeof(struct sr_datafeed_header);
369 packet.payload = (unsigned char *) &header;
370 header.feed_version = 1;
371 gettimeofday(&header.starttime, NULL);
372 header.samplerate = alsa->cur_rate;
373 header.num_analog_probes = NUM_PROBES;
374 header.num_logic_probes = 0;
375 header.protocol_id = SR_PROTO_RAW;
376 sr_session_bus(session_device_id, &packet);
382 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
384 /* Avoid compiler warnings. */
385 device_index = device_index;
386 session_device_id = session_device_id;
389 SR_PRIV struct sr_device_plugin alsa_plugin_info = {
391 .longname = "ALSA driver",
394 .cleanup = hw_cleanup,
395 .opendev = hw_opendev,
396 .closedev = hw_closedev,
397 .get_device_info = hw_get_device_info,
398 .get_status = hw_get_status,
399 .get_capabilities = hw_get_capabilities,
400 .set_configuration = hw_set_configuration,
401 .start_acquisition = hw_start_acquisition,
402 .stop_acquisition = hw_stop_acquisition,