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 #include "config.h" /* Must come before sigrok.h */
25 #include <alsa/asoundlib.h>
27 #include "sigrok-internal.h"
30 #define SAMPLE_WIDTH 16
31 #define AUDIO_DEV "plughw:0,0"
33 static int capabilities[] = {
35 SR_HWCAP_LIMIT_SAMPLES,
39 static GSList *device_instances = NULL;
43 uint64_t limit_samples;
44 snd_pcm_t *capture_handle;
45 snd_pcm_hw_params_t *hw_params;
49 static int hw_init(const char *deviceinfo)
51 struct sr_device_instance *sdi;
54 /* Avoid compiler warnings. */
55 deviceinfo = deviceinfo;
57 if (!(alsa = g_try_malloc0(sizeof(struct alsa)))) {
58 sr_err("alsa: %s: alsa malloc failed", __func__);
62 sdi = sr_device_instance_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL);
68 device_instances = g_slist_append(device_instances, sdi);
76 static int hw_opendev(int device_index)
78 struct sr_device_instance *sdi;
82 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
86 err = snd_pcm_open(&alsa->capture_handle, AUDIO_DEV,
87 SND_PCM_STREAM_CAPTURE, 0);
89 sr_warn("cannot open audio device %s (%s)", AUDIO_DEV,
94 err = snd_pcm_hw_params_malloc(&alsa->hw_params);
96 sr_warn("cannot allocate hardware parameter structure (%s)",
101 err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params);
103 sr_warn("cannot initialize hardware parameter structure (%s)",
111 static int hw_closedev(int device_index)
113 struct sr_device_instance *sdi;
116 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
117 sr_err("alsa: %s: sdi was NULL", __func__);
118 return SR_ERR; /* TODO: SR_ERR_ARG? */
121 if (!(alsa = sdi->priv)) {
122 sr_err("alsa: %s: sdi->priv was NULL", __func__);
123 return SR_ERR; /* TODO: SR_ERR_ARG? */
126 // TODO: Return values of snd_*?
128 snd_pcm_hw_params_free(alsa->hw_params);
129 if (alsa->capture_handle)
130 snd_pcm_close(alsa->capture_handle);
135 static void hw_cleanup(void)
137 struct sr_device_instance *sdi;
139 if (!(sdi = sr_get_device_instance(device_instances, 0)))
143 sr_device_instance_free(sdi);
146 static void *hw_get_device_info(int device_index, int device_info_id)
148 struct sr_device_instance *sdi;
152 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
156 switch (device_info_id) {
160 case SR_DI_NUM_PROBES:
161 info = GINT_TO_POINTER(NUM_PROBES);
163 case SR_DI_CUR_SAMPLERATE:
164 info = &alsa->cur_rate;
166 // case SR_DI_PROBE_TYPE:
167 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
174 static int hw_get_status(int device_index)
176 /* Avoid compiler warnings. */
177 device_index = device_index;
182 static int *hw_get_capabilities(void)
187 static int hw_set_configuration(int device_index, int capability, void *value)
189 struct sr_device_instance *sdi;
192 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
196 switch (capability) {
197 case SR_HWCAP_PROBECONFIG:
199 case SR_HWCAP_SAMPLERATE:
200 alsa->cur_rate = *(uint64_t *) value;
202 case SR_HWCAP_LIMIT_SAMPLES:
203 alsa->limit_samples = *(uint64_t *) value;
210 static int receive_data(int fd, int revents, void *user_data)
212 struct sr_device_instance *sdi = user_data;
213 struct alsa *alsa = sdi->priv;
214 struct sr_datafeed_packet packet;
215 struct sr_analog_sample *sample;
216 unsigned int sample_size = sizeof(struct sr_analog_sample) +
217 (NUM_PROBES * sizeof(struct sr_analog_probe));
226 memset(inb, 0, sizeof(inb));
227 count = snd_pcm_readi(alsa->capture_handle, inb,
228 MIN(4096/4, alsa->limit_samples));
230 sr_warn("Failed to read samples");
234 if (!(outb = g_try_malloc(sample_size * count))) {
235 sr_err("alsa: %s: outb malloc failed", __func__);
239 for (i = 0; i < count; i++) {
240 sample = (struct sr_analog_sample *)
241 (outb + (i * sample_size));
242 sample->num_probes = NUM_PROBES;
244 for (x = 0; x < NUM_PROBES; x++) {
245 sample->probes[x].val =
246 *(uint16_t *) (inb + (i * 4) + (x * 2));
247 sample->probes[x].val &= ((1 << 16) - 1);
248 sample->probes[x].res = 16;
252 packet.type = SR_DF_ANALOG;
253 packet.length = count * sample_size;
254 packet.unitsize = sample_size;
255 packet.payload = outb;
256 sr_session_bus(user_data, &packet);
258 alsa->limit_samples -= count;
260 } while (alsa->limit_samples > 0);
262 packet.type = SR_DF_END;
263 sr_session_bus(user_data, &packet);
268 static int hw_start_acquisition(int device_index, gpointer session_device_id)
270 struct sr_device_instance *sdi;
272 struct sr_datafeed_packet packet;
273 struct sr_datafeed_header header;
278 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
282 err = snd_pcm_hw_params_set_access(alsa->capture_handle,
283 alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
285 sr_warn("cannot set access type (%s)", snd_strerror(err));
289 /* FIXME: Hardcoded for 16bits */
290 err = snd_pcm_hw_params_set_format(alsa->capture_handle,
291 alsa->hw_params, SND_PCM_FORMAT_S16_LE);
293 sr_warn("cannot set sample format (%s)", snd_strerror(err));
297 err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle,
298 alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0);
300 sr_warn("cannot set sample rate (%s)", snd_strerror(err));
304 err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
305 alsa->hw_params, NUM_PROBES);
307 sr_warn("cannot set channel count (%s)", snd_strerror(err));
311 err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
313 sr_warn("cannot set parameters (%s)", snd_strerror(err));
317 err = snd_pcm_prepare(alsa->capture_handle);
319 sr_warn("cannot prepare audio interface for use (%s)",
324 count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
326 sr_warn("Unable to obtain poll descriptors count");
330 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
331 sr_warn("alsa: %s: ufds malloc failed", __func__);
332 return SR_ERR_MALLOC;
335 err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
337 sr_warn("Unable to obtain poll descriptors (%s)",
343 alsa->session_id = session_device_id;
344 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
346 packet.type = SR_DF_HEADER;
347 packet.length = sizeof(struct sr_datafeed_header);
348 packet.payload = (unsigned char *) &header;
349 header.feed_version = 1;
350 gettimeofday(&header.starttime, NULL);
351 header.samplerate = alsa->cur_rate;
352 header.num_analog_probes = NUM_PROBES;
353 header.num_logic_probes = 0;
354 header.protocol_id = SR_PROTO_RAW;
355 sr_session_bus(session_device_id, &packet);
361 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
363 /* Avoid compiler warnings. */
364 device_index = device_index;
365 session_device_id = session_device_id;
368 struct sr_device_plugin alsa_plugin_info = {
370 .longname = "ALSA driver",
373 .cleanup = hw_cleanup,
374 .opendev = hw_opendev,
375 .closedev = hw_closedev,
376 .get_device_info = hw_get_device_info,
377 .get_status = hw_get_status,
378 .get_capabilities = hw_get_capabilities,
379 .set_configuration = hw_set_configuration,
380 .start_acquisition = hw_start_acquisition,
381 .stop_acquisition = hw_stop_acquisition,