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