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,
snd_pcm_t * capture_handle
The device supports continuous sampling, i.e.
struct sr_analog_probe probes[]
snd_pcm_hw_params_t * hw_params
The device supports setting a sample number limit, i.e.
SR_PRIV int sr_source_add(int fd, int events, int timeout, sr_receive_data_callback_t cb, void *cb_data)
void * session_dev_id
TODO.
SR_PRIV struct sr_dev_inst * sr_dev_inst_get(GSList *dev_insts, int dev_index)
SR_PRIV struct sr_dev_inst * sr_dev_inst_new(int index, int status, const char *vendor, const char *model, const char *version)
The device supports setting/changing its samplerate.
The device supports setting a probe mask.
SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
SR_PRIV struct sr_dev_driver alsa_driver_info
uint64_t limit_samples
The current sampling limit (in number of samples).
SR_PRIV int sr_session_send(struct sr_dev *dev, struct sr_datafeed_packet *packet)
Send a packet to whatever is listening on the datafeed bus.
SR_PRIV int sr_err(const char *format,...)