]> sigrok.org Git - libsigrok.git/blame - hardware/alsa/alsa.c
probe names: Fix cosmetics, add docs, fix off-by-one.
[libsigrok.git] / hardware / alsa / alsa.c
CommitLineData
6ed4f044
DR
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
92b3101c 21#include "config.h" /* Must come before sigrok.h */
6ed4f044
DR
22#include <stdlib.h>
23#include <unistd.h>
24#include <string.h>
6ed4f044 25#include <alsa/asoundlib.h>
b7f09cf8
UH
26#include "sigrok.h"
27#include "sigrok-internal.h"
6ed4f044
DR
28
29#define NUM_PROBES 2
30#define SAMPLE_WIDTH 16
31#define AUDIO_DEV "plughw:0,0"
32
33static int capabilities[] = {
5a2326a7
UH
34 SR_HWCAP_SAMPLERATE,
35 SR_HWCAP_LIMIT_SAMPLES,
36 SR_HWCAP_CONTINUOUS,
6ed4f044
DR
37};
38
c37d2b1b 39static const char *probe_names[NUM_PROBES + 1] = {
464d12c7
KS
40 "0",
41 "1",
42 NULL,
43};
44
6ed4f044
DR
45static GSList *device_instances = NULL;
46
47struct alsa {
48 uint64_t cur_rate;
49 uint64_t limit_samples;
50 snd_pcm_t *capture_handle;
51 snd_pcm_hw_params_t *hw_params;
52 gpointer session_id;
53};
54
54ac5277 55static int hw_init(const char *deviceinfo)
6ed4f044 56{
a00ba012 57 struct sr_device_instance *sdi;
6ed4f044
DR
58 struct alsa *alsa;
59
60 /* Avoid compiler warnings. */
61 deviceinfo = deviceinfo;
62
92b3101c
UH
63 if (!(alsa = g_try_malloc0(sizeof(struct alsa)))) {
64 sr_err("alsa: %s: alsa malloc failed", __func__);
6ed4f044 65 return 0;
92b3101c 66 }
6ed4f044 67
5a2326a7 68 sdi = sr_device_instance_new(0, SR_ST_ACTIVE, "alsa", NULL, NULL);
6ed4f044
DR
69 if (!sdi)
70 goto free_alsa;
71
72 sdi->priv = alsa;
73
74 device_instances = g_slist_append(device_instances, sdi);
75
76 return 1;
77free_alsa:
92b3101c 78 g_free(alsa);
6ed4f044
DR
79 return 0;
80}
81
82static int hw_opendev(int device_index)
83{
a00ba012 84 struct sr_device_instance *sdi;
6ed4f044
DR
85 struct alsa *alsa;
86 int err;
87
d32d961d 88 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 89 return SR_ERR;
6ed4f044
DR
90 alsa = sdi->priv;
91
92 err = snd_pcm_open(&alsa->capture_handle, AUDIO_DEV,
93 SND_PCM_STREAM_CAPTURE, 0);
94 if (err < 0) {
b08024a8 95 sr_warn("cannot open audio device %s (%s)", AUDIO_DEV,
92b3101c 96 snd_strerror(err));
e46b8fb1 97 return SR_ERR;
6ed4f044
DR
98 }
99
100 err = snd_pcm_hw_params_malloc(&alsa->hw_params);
101 if (err < 0) {
b08024a8 102 sr_warn("cannot allocate hardware parameter structure (%s)",
92b3101c 103 snd_strerror(err));
e46b8fb1 104 return SR_ERR;
6ed4f044
DR
105 }
106
107 err = snd_pcm_hw_params_any(alsa->capture_handle, alsa->hw_params);
108 if (err < 0) {
b08024a8 109 sr_warn("cannot initialize hardware parameter structure (%s)",
6ed4f044 110 snd_strerror(err));
e46b8fb1 111 return SR_ERR;
6ed4f044
DR
112 }
113
e46b8fb1 114 return SR_OK;
6ed4f044
DR
115}
116
697785d1 117static int hw_closedev(int device_index)
6ed4f044 118{
a00ba012 119 struct sr_device_instance *sdi;
6ed4f044
DR
120 struct alsa *alsa;
121
697785d1
UH
122 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
123 sr_err("alsa: %s: sdi was NULL", __func__);
124 return SR_ERR; /* TODO: SR_ERR_ARG? */
125 }
6ed4f044 126
697785d1
UH
127 if (!(alsa = sdi->priv)) {
128 sr_err("alsa: %s: sdi->priv was NULL", __func__);
129 return SR_ERR; /* TODO: SR_ERR_ARG? */
130 }
131
132 // TODO: Return values of snd_*?
6ed4f044
DR
133 if (alsa->hw_params)
134 snd_pcm_hw_params_free(alsa->hw_params);
135 if (alsa->capture_handle)
136 snd_pcm_close(alsa->capture_handle);
697785d1
UH
137
138 return SR_OK;
6ed4f044
DR
139}
140
141static void hw_cleanup(void)
142{
a00ba012 143 struct sr_device_instance *sdi;
6ed4f044 144
d32d961d 145 if (!(sdi = sr_get_device_instance(device_instances, 0)))
6ed4f044
DR
146 return;
147
148 free(sdi->priv);
a00ba012 149 sr_device_instance_free(sdi);
6ed4f044
DR
150}
151
152static void *hw_get_device_info(int device_index, int device_info_id)
153{
a00ba012 154 struct sr_device_instance *sdi;
6ed4f044
DR
155 struct alsa *alsa;
156 void *info = NULL;
157
d32d961d 158 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
6ed4f044
DR
159 return NULL;
160 alsa = sdi->priv;
161
162 switch (device_info_id) {
5a2326a7 163 case SR_DI_INSTANCE:
6ed4f044
DR
164 info = sdi;
165 break;
5a2326a7 166 case SR_DI_NUM_PROBES:
6ed4f044
DR
167 info = GINT_TO_POINTER(NUM_PROBES);
168 break;
464d12c7
KS
169 case SR_DI_PROBE_NAMES:
170 info = probe_names;
171 break;
5a2326a7 172 case SR_DI_CUR_SAMPLERATE:
6ed4f044
DR
173 info = &alsa->cur_rate;
174 break;
5a2326a7
UH
175 // case SR_DI_PROBE_TYPE:
176 // info = GINT_TO_POINTER(SR_PROBE_TYPE_ANALOG);
da692373 177 // break;
6ed4f044
DR
178 }
179
180 return info;
181}
182
183static int hw_get_status(int device_index)
184{
185 /* Avoid compiler warnings. */
186 device_index = device_index;
187
5a2326a7 188 return SR_ST_ACTIVE;
6ed4f044
DR
189}
190
191static int *hw_get_capabilities(void)
192{
193 return capabilities;
194}
195
196static int hw_set_configuration(int device_index, int capability, void *value)
197{
a00ba012 198 struct sr_device_instance *sdi;
6ed4f044
DR
199 struct alsa *alsa;
200
d32d961d 201 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 202 return SR_ERR;
6ed4f044
DR
203 alsa = sdi->priv;
204
205 switch (capability) {
5a2326a7 206 case SR_HWCAP_PROBECONFIG:
e46b8fb1 207 return SR_OK;
5a2326a7 208 case SR_HWCAP_SAMPLERATE:
6ed4f044 209 alsa->cur_rate = *(uint64_t *) value;
e46b8fb1 210 return SR_OK;
5a2326a7 211 case SR_HWCAP_LIMIT_SAMPLES:
6ed4f044 212 alsa->limit_samples = *(uint64_t *) value;
e46b8fb1 213 return SR_OK;
6ed4f044 214 default:
e46b8fb1 215 return SR_ERR;
6ed4f044
DR
216 }
217}
218
6ed4f044
DR
219static int receive_data(int fd, int revents, void *user_data)
220{
a00ba012 221 struct sr_device_instance *sdi = user_data;
6ed4f044 222 struct alsa *alsa = sdi->priv;
b9c735a2 223 struct sr_datafeed_packet packet;
809c5f20
UH
224 struct sr_analog_sample *sample;
225 unsigned int sample_size = sizeof(struct sr_analog_sample) +
226 (NUM_PROBES * sizeof(struct sr_analog_probe));
58330ab8
DR
227 char *outb;
228 char inb[4096];
229 int i, x, count;
230
cdbc51d9
DR
231 fd = fd;
232 revents = revents;
233
58330ab8
DR
234 do {
235 memset(inb, 0, sizeof(inb));
236 count = snd_pcm_readi(alsa->capture_handle, inb,
237 MIN(4096/4, alsa->limit_samples));
238 if (count < 1) {
b08024a8 239 sr_warn("Failed to read samples");
58330ab8
DR
240 return FALSE;
241 }
6ed4f044 242
92b3101c
UH
243 if (!(outb = g_try_malloc(sample_size * count))) {
244 sr_err("alsa: %s: outb malloc failed", __func__);
6ed4f044 245 return FALSE;
92b3101c 246 }
58330ab8
DR
247
248 for (i = 0; i < count; i++) {
809c5f20 249 sample = (struct sr_analog_sample *)
58330ab8
DR
250 (outb + (i * sample_size));
251 sample->num_probes = NUM_PROBES;
252
253 for (x = 0; x < NUM_PROBES; x++) {
254 sample->probes[x].val =
255 *(uint16_t *) (inb + (i * 4) + (x * 2));
256 sample->probes[x].val &= ((1 << 16) - 1);
257 sample->probes[x].res = 16;
258 }
6ed4f044 259 }
58330ab8 260
5a2326a7 261 packet.type = SR_DF_ANALOG;
58330ab8
DR
262 packet.length = count * sample_size;
263 packet.unitsize = sample_size;
264 packet.payload = outb;
8a2efef2 265 sr_session_bus(user_data, &packet);
92b3101c 266 g_free(outb);
58330ab8
DR
267 alsa->limit_samples -= count;
268
269 } while (alsa->limit_samples > 0);
270
5a2326a7 271 packet.type = SR_DF_END;
8a2efef2 272 sr_session_bus(user_data, &packet);
6ed4f044
DR
273
274 return TRUE;
275}
276
277static int hw_start_acquisition(int device_index, gpointer session_device_id)
278{
a00ba012 279 struct sr_device_instance *sdi;
6ed4f044 280 struct alsa *alsa;
b9c735a2
UH
281 struct sr_datafeed_packet packet;
282 struct sr_datafeed_header header;
6ed4f044
DR
283 struct pollfd *ufds;
284 int count;
285 int err;
286
d32d961d 287 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 288 return SR_ERR;
6ed4f044
DR
289 alsa = sdi->priv;
290
291 err = snd_pcm_hw_params_set_access(alsa->capture_handle,
292 alsa->hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
293 if (err < 0) {
b08024a8 294 sr_warn("cannot set access type (%s)", snd_strerror(err));
e46b8fb1 295 return SR_ERR;
6ed4f044
DR
296 }
297
298 /* FIXME: Hardcoded for 16bits */
299 err = snd_pcm_hw_params_set_format(alsa->capture_handle,
300 alsa->hw_params, SND_PCM_FORMAT_S16_LE);
301 if (err < 0) {
b08024a8 302 sr_warn("cannot set sample format (%s)", snd_strerror(err));
e46b8fb1 303 return SR_ERR;
6ed4f044
DR
304 }
305
306 err = snd_pcm_hw_params_set_rate_near(alsa->capture_handle,
307 alsa->hw_params, (unsigned int *) &alsa->cur_rate, 0);
308 if (err < 0) {
b08024a8 309 sr_warn("cannot set sample rate (%s)", snd_strerror(err));
e46b8fb1 310 return SR_ERR;
6ed4f044
DR
311 }
312
313 err = snd_pcm_hw_params_set_channels(alsa->capture_handle,
314 alsa->hw_params, NUM_PROBES);
315 if (err < 0) {
b08024a8 316 sr_warn("cannot set channel count (%s)", snd_strerror(err));
e46b8fb1 317 return SR_ERR;
6ed4f044
DR
318 }
319
320 err = snd_pcm_hw_params(alsa->capture_handle, alsa->hw_params);
321 if (err < 0) {
b08024a8 322 sr_warn("cannot set parameters (%s)", snd_strerror(err));
e46b8fb1 323 return SR_ERR;
6ed4f044
DR
324 }
325
326 err = snd_pcm_prepare(alsa->capture_handle);
327 if (err < 0) {
b08024a8 328 sr_warn("cannot prepare audio interface for use (%s)",
6ed4f044 329 snd_strerror(err));
e46b8fb1 330 return SR_ERR;
6ed4f044
DR
331 }
332
333 count = snd_pcm_poll_descriptors_count(alsa->capture_handle);
334 if (count < 1) {
b08024a8 335 sr_warn("Unable to obtain poll descriptors count");
e46b8fb1 336 return SR_ERR;
6ed4f044
DR
337 }
338
92b3101c
UH
339 if (!(ufds = g_try_malloc(count * sizeof(struct pollfd)))) {
340 sr_warn("alsa: %s: ufds malloc failed", __func__);
e46b8fb1 341 return SR_ERR_MALLOC;
92b3101c 342 }
6ed4f044
DR
343
344 err = snd_pcm_poll_descriptors(alsa->capture_handle, ufds, count);
345 if (err < 0) {
b08024a8 346 sr_warn("Unable to obtain poll descriptors (%s)",
6ed4f044 347 snd_strerror(err));
92b3101c 348 g_free(ufds);
e46b8fb1 349 return SR_ERR;
6ed4f044
DR
350 }
351
352 alsa->session_id = session_device_id;
6f1be0a2 353 sr_source_add(ufds[0].fd, ufds[0].events, 10, receive_data, sdi);
6ed4f044 354
5a2326a7 355 packet.type = SR_DF_HEADER;
b9c735a2 356 packet.length = sizeof(struct sr_datafeed_header);
6ed4f044
DR
357 packet.payload = (unsigned char *) &header;
358 header.feed_version = 1;
359 gettimeofday(&header.starttime, NULL);
360 header.samplerate = alsa->cur_rate;
361 header.num_analog_probes = NUM_PROBES;
362 header.num_logic_probes = 0;
5a2326a7 363 header.protocol_id = SR_PROTO_RAW;
8a2efef2 364 sr_session_bus(session_device_id, &packet);
92b3101c 365 g_free(ufds);
6ed4f044 366
e46b8fb1 367 return SR_OK;
6ed4f044
DR
368}
369
370static void hw_stop_acquisition(int device_index, gpointer session_device_id)
371{
372 /* Avoid compiler warnings. */
373 device_index = device_index;
374 session_device_id = session_device_id;
375}
376
5c2d46d1 377struct sr_device_plugin alsa_plugin_info = {
e519ba86
UH
378 .name = "alsa",
379 .longname = "ALSA driver",
380 .api_version = 1,
381 .init = hw_init,
382 .cleanup = hw_cleanup,
86f5e3d8
UH
383 .opendev = hw_opendev,
384 .closedev = hw_closedev,
e519ba86
UH
385 .get_device_info = hw_get_device_info,
386 .get_status = hw_get_status,
387 .get_capabilities = hw_get_capabilities,
388 .set_configuration = hw_set_configuration,
389 .start_acquisition = hw_start_acquisition,
390 .stop_acquisition = hw_stop_acquisition,
6ed4f044 391};