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