26 #include <alsa/asoundlib.h>
28 #include "sigrok-internal.h"
31 #define SAMPLE_WIDTH 16
32 #define AUDIO_DEV "plughw:0,0"
45 static int hwcaps[] = {
52 static const char *probe_names[
NUM_PROBES + 1] = {
58 static GSList *dev_insts = NULL;
69 static int hw_init(
const char *devinfo)
77 if (!(ctx = g_try_malloc0(
sizeof(
struct context)))) {
78 sr_err(
"alsa: %s: ctx malloc failed", __func__);
83 sr_err(
"alsa: %s: sdi was NULL", __func__);
89 dev_insts = g_slist_append(dev_insts, sdi);
109 SND_PCM_STREAM_CAPTURE, 0);
116 ret = snd_pcm_hw_params_malloc(&ctx->
hw_params);
118 sr_err(
"alsa: can't allocate hardware parameter structure (%s)",
125 sr_err(
"alsa: can't initialize hardware parameter structure "
126 "(%s)", snd_strerror(ret));
133 static int hw_dev_close(
int 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__);
157 static int hw_cleanup(
void)
162 sr_err(
"alsa: %s: sdi was NULL", __func__);
171 static void *hw_dev_info_get(
int dev_index,
int dev_info_id)
181 switch (dev_info_id) {
202 static int hw_dev_status_get(
int dev_index)
210 static int *hw_hwcap_get_all(
void)
215 static int hw_dev_config_set(
int dev_index,
int hwcap,
void *value)
238 static int receive_data(
int fd,
int revents,
void *cb_data)
254 memset(inb, 0,
sizeof(inb));
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++) {
269 (outb + (i * sample_size));
274 *(uint16_t *)(inb + (i * 4) + (x * 2));
275 sample->
probes[x].
val &= ((1 << 16) - 1);
280 packet.type = SR_DF_ANALOG;
281 packet.length = count * sample_size;
282 packet.unitsize = sample_size;
283 packet.payload = outb;
296 static int hw_dev_acquisition_start(
int dev_index,
void *cb_data)
311 ctx->
hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
313 sr_err(
"alsa: can't set access type (%s)", snd_strerror(ret));
321 sr_err(
"alsa: can't set sample format (%s)", snd_strerror(ret));
328 sr_err(
"alsa: can't set sample rate (%s)", snd_strerror(ret));
335 sr_err(
"alsa: can't set channel count (%s)", snd_strerror(ret));
341 sr_err(
"alsa: can't set parameters (%s)", snd_strerror(ret));
347 sr_err(
"alsa: can't prepare audio interface for use (%s)",
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__);
365 sr_err(
"alsa: Unable to obtain poll descriptors (%s)",
372 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
376 packet.payload = (
unsigned char *)&header;
377 header.feed_version = 1;
378 gettimeofday(&header.starttime, NULL);
381 header.num_logic_probes = 0;
382 header.protocol_id = SR_PROTO_RAW;
390 static int hw_dev_acquisition_stop(
int dev_index,
void *cb_data)
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,