]> sigrok.org Git - libsigrok.git/blame_incremental - alsa.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / alsa.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5 *
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.
10 *
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.
15 *
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
19 */
20
21/* Note: This driver doesn't compile, analog support in sigrok is WIP. */
22
23#include <stdlib.h>
24#include <unistd.h>
25#include <string.h>
26#include <alsa/asoundlib.h>
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
29
30#define NUM_PROBES 2
31#define SAMPLE_WIDTH 16
32#define AUDIO_DEV "plughw:0,0"
33
34struct sr_analog_probe {
35 uint8_t att;
36 uint8_t res; /* Needs to be a power of 2, FIXME */
37 uint16_t val; /* Max hardware ADC width is 16bits */
38};
39
40struct sr_analog_sample {
41 uint8_t num_probes; /* Max hardware probes is 256 */
42 struct sr_analog_probe probes[];
43};
44
45static const int hwcaps[] = {
46 SR_HWCAP_SAMPLERATE,
47 SR_HWCAP_LIMIT_SAMPLES,
48 SR_HWCAP_CONTINUOUS,
49};
50
51/* TODO: Which probe names/numbers to use? */
52static const char *probe_names[NUM_PROBES + 1] = {
53 "0",
54 "1",
55 NULL,
56};
57
58static GSList *dev_insts = NULL;
59
60/* Private, per-device-instance driver context. */
61struct context {
62 uint64_t cur_rate;
63 uint64_t limit_samples;
64 snd_pcm_t *capture_handle;
65 snd_pcm_hw_params_t *hw_params;
66 void *session_dev_id;
67};
68
69static int hw_init(const char *devinfo)
70{
71 struct sr_dev_inst *sdi;
72 struct context *ctx;
73
74 (void)devinfo;
75
76 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
77 sr_err("alsa: %s: ctx malloc failed", __func__);
78 return SR_ERR_MALLOC;
79 }
80
81 if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL))) {
82 sr_err("alsa: %s: sdi was NULL", __func__);
83 goto free_ctx;
84 }
85
86 sdi->priv = ctx;
87
88 dev_insts = g_slist_append(dev_insts, sdi);
89
90 return 1;
91
92free_ctx:
93 g_free(ctx);
94 return 0;
95}
96
97static int hw_dev_open(int dev_index)
98{
99 struct sr_dev_inst *sdi;
100 struct context *ctx;
101 int ret;
102
103 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
104 return SR_ERR;
105 ctx = sdi->priv;
106
107 ret = snd_pcm_open(&ctx->capture_handle, AUDIO_DEV,
108 SND_PCM_STREAM_CAPTURE, 0);
109 if (ret < 0) {
110 sr_err("alsa: can't open audio device %s (%s)", AUDIO_DEV,
111 snd_strerror(ret));
112 return SR_ERR;
113 }
114
115 ret = snd_pcm_hw_params_malloc(&ctx->hw_params);
116 if (ret < 0) {
117 sr_err("alsa: can't allocate hardware parameter structure (%s)",
118 snd_strerror(ret));
119 return SR_ERR_MALLOC;
120 }
121
122 ret = snd_pcm_hw_params_any(ctx->capture_handle, ctx->hw_params);
123 if (ret < 0) {
124 sr_err("alsa: can't initialize hardware parameter structure "
125 "(%s)", snd_strerror(ret));
126 return SR_ERR;
127 }
128
129 return SR_OK;
130}
131
132static int hw_dev_close(int dev_index)
133{
134 struct sr_dev_inst *sdi;
135 struct context *ctx;
136
137 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
138 sr_err("alsa: %s: sdi was NULL", __func__);
139 return SR_ERR_BUG;
140 }
141
142 if (!(ctx = sdi->priv)) {
143 sr_err("alsa: %s: sdi->priv was NULL", __func__);
144 return SR_ERR_BUG;
145 }
146
147 // TODO: Return values of snd_*?
148 if (ctx->hw_params)
149 snd_pcm_hw_params_free(ctx->hw_params);
150 if (ctx->capture_handle)
151 snd_pcm_close(ctx->capture_handle);
152
153 return SR_OK;
154}
155
156static int hw_cleanup(void)
157{
158 struct sr_dev_inst *sdi;
159
160 if (!(sdi = sr_dev_inst_get(dev_insts, 0))) {
161 sr_err("alsa: %s: sdi was NULL", __func__);
162 return SR_ERR_BUG;
163 }
164
165 sr_dev_inst_free(sdi);
166
167 return SR_OK;
168}
169
170static const void *hw_dev_info_get(int dev_index, int dev_info_id)
171{
172 struct sr_dev_inst *sdi;
173 struct context *ctx;
174 void *info = NULL;
175
176 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
177 return NULL;
178 ctx = sdi->priv;
179
180 switch (dev_info_id) {
181 case SR_DI_INST:
182 info = sdi;
183 break;
184 case SR_DI_NUM_PROBES:
185 info = GINT_TO_POINTER(NUM_PROBES);
186 break;
187 case SR_DI_PROBE_NAMES:
188 info = probe_names;
189 break;
190 case SR_DI_CUR_SAMPLERATE:
191 info = &ctx->cur_rate;
192 break;
193 // case SR_DI_PROBE_TYPE:
194 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
195 // break;
196 }
197
198 return info;
199}
200
201static int hw_dev_status_get(int dev_index)
202{
203 /* Avoid compiler warnings. */
204 dev_index = dev_index;
205
206 return SR_ST_ACTIVE;
207}
208
209static const int *hw_hwcap_get_all(void)
210{
211 return hwcaps;
212}
213
214static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
215{
216 struct sr_dev_inst *sdi;
217 struct context *ctx;
218
219 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
220 return SR_ERR;
221 ctx = sdi->priv;
222
223 switch (hwcap) {
224 case SR_HWCAP_PROBECONFIG:
225 return SR_OK;
226 case SR_HWCAP_SAMPLERATE:
227 ctx->cur_rate = *(const uint64_t *)value;
228 return SR_OK;
229 case SR_HWCAP_LIMIT_SAMPLES:
230 ctx->limit_samples = *(const uint64_t *)value;
231 return SR_OK;
232 default:
233 return SR_ERR;
234 }
235}
236
237static int receive_data(int fd, int revents, void *cb_data)
238{
239 struct sr_dev_inst *sdi = cb_data;
240 struct context *ctx = sdi->priv;
241 struct sr_datafeed_packet packet;
242 struct sr_analog_sample *sample;
243 unsigned int sample_size = sizeof(struct sr_analog_sample) +
244 (NUM_PROBES * sizeof(struct sr_analog_probe));
245 char *outb;
246 char inb[4096];
247 int i, x, count;
248
249 fd = fd;
250 revents = revents;
251
252 do {
253 memset(inb, 0, sizeof(inb));
254 count = snd_pcm_readi(ctx->capture_handle, inb,
255 MIN(4096 / 4, ctx->limit_samples));
256 if (count < 1) {
257 sr_err("alsa: Failed to read samples");
258 return FALSE;
259 }
260
261 if (!(outb = g_try_malloc(sample_size * count))) {
262 sr_err("alsa: %s: outb malloc failed", __func__);
263 return FALSE;
264 }
265
266 for (i = 0; i < count; i++) {
267 sample = (struct sr_analog_sample *)
268 (outb + (i * sample_size));
269 sample->num_probes = NUM_PROBES;
270
271 for (x = 0; x < NUM_PROBES; x++) {
272 sample->probes[x].val =
273 *(uint16_t *)(inb + (i * 4) + (x * 2));
274 sample->probes[x].val &= ((1 << 16) - 1);
275 sample->probes[x].res = 16;
276 }
277 }
278
279 packet.type = SR_DF_ANALOG;
280 packet.length = count * sample_size;
281 packet.unitsize = sample_size;
282 packet.payload = outb;
283 sr_session_send(sdi, &packet);
284 g_free(outb);
285 ctx->limit_samples -= count;
286
287 } while (ctx->limit_samples > 0);
288
289 packet.type = SR_DF_END;
290 sr_session_send(sdi, &packet);
291
292 return TRUE;
293}
294
295static int hw_dev_acquisition_start(int dev_index, void *cb_data)
296{
297 struct sr_dev_inst *sdi;
298 struct context *ctx;
299 struct sr_datafeed_packet packet;
300 struct sr_datafeed_header header;
301 struct pollfd *ufds;
302 int count;
303 int ret;
304
305 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
306 return SR_ERR;
307 ctx = sdi->priv;
308
309 ret = snd_pcm_hw_params_set_access(ctx->capture_handle,
310 ctx->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
311 if (ret < 0) {
312 sr_err("alsa: can't set access type (%s)", snd_strerror(ret));
313 return SR_ERR;
314 }
315
316 /* FIXME: Hardcoded for 16bits */
317 ret = snd_pcm_hw_params_set_format(ctx->capture_handle,
318 ctx->hw_params, SND_PCM_FORMAT_S16_LE);
319 if (ret < 0) {
320 sr_err("alsa: can't set sample format (%s)", snd_strerror(ret));
321 return SR_ERR;
322 }
323
324 ret = snd_pcm_hw_params_set_rate_near(ctx->capture_handle,
325 ctx->hw_params, (unsigned int *)&ctx->cur_rate, 0);
326 if (ret < 0) {
327 sr_err("alsa: can't set sample rate (%s)", snd_strerror(ret));
328 return SR_ERR;
329 }
330
331 ret = snd_pcm_hw_params_set_channels(ctx->capture_handle,
332 ctx->hw_params, NUM_PROBES);
333 if (ret < 0) {
334 sr_err("alsa: can't set channel count (%s)", snd_strerror(ret));
335 return SR_ERR;
336 }
337
338 ret = snd_pcm_hw_params(ctx->capture_handle, ctx->hw_params);
339 if (ret < 0) {
340 sr_err("alsa: can't set parameters (%s)", snd_strerror(ret));
341 return SR_ERR;
342 }
343
344 ret = snd_pcm_prepare(ctx->capture_handle);
345 if (ret < 0) {
346 sr_err("alsa: can't prepare audio interface for use (%s)",
347 snd_strerror(ret));
348 return SR_ERR;
349 }
350
351 count = snd_pcm_poll_descriptors_count(ctx->capture_handle);
352 if (count < 1) {
353 sr_err("alsa: Unable to obtain poll descriptors count");
354 return SR_ERR;
355 }
356
357 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
358 sr_err("alsa: %s: ufds malloc failed", __func__);
359 return SR_ERR_MALLOC;
360 }
361
362 ret = snd_pcm_poll_descriptors(ctx->capture_handle, ufds, count);
363 if (ret < 0) {
364 sr_err("alsa: Unable to obtain poll descriptors (%s)",
365 snd_strerror(ret));
366 g_free(ufds);
367 return SR_ERR;
368 }
369
370 ctx->session_dev_id = cb_data;
371 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
372
373 packet.type = SR_DF_HEADER;
374 packet.length = sizeof(struct sr_datafeed_header);
375 packet.payload = (unsigned char *)&header;
376 header.feed_version = 1;
377 gettimeofday(&header.starttime, NULL);
378 header.samplerate = ctx->cur_rate;
379 header.num_analog_probes = NUM_PROBES;
380 header.num_logic_probes = 0;
381 header.protocol_id = SR_PROTO_RAW;
382 sr_session_send(cb_data, &packet);
383 g_free(ufds);
384
385 return SR_OK;
386}
387
388/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
389static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
390{
391 (void)dev_index;
392 (void)cb_data;
393
394 return SR_OK;
395}
396
397SR_PRIV struct sr_dev_driver alsa_driver_info = {
398 .name = "alsa",
399 .longname = "ALSA driver",
400 .api_version = 1,
401 .init = hw_init,
402 .cleanup = hw_cleanup,
403 .dev_open = hw_dev_open,
404 .dev_close = hw_dev_close,
405 .dev_info_get = hw_dev_info_get,
406 .dev_status_get = hw_dev_status_get,
407 .hwcap_get_all = hw_hwcap_get_all,
408 .dev_config_set = hw_dev_config_set,
409 .dev_acquisition_start = hw_dev_acquisition_start,
410 .dev_acquisition_stop = hw_dev_acquisition_stop,
411};